首页 > 解决方案 > 在 Kotlin 中使用“when”语句时,如何为每种情况调用多个函数?

问题描述

我有一个由 8 个按钮组成的链,我定位为侧菜单,在它们的右侧有一个片段容器。单击每个按钮会打开一个不同的片段。

这是通过 onClick 侦听器和 when 语句完成的。

我想要做的是除了更改框中的片段之外,我希望每个按钮的背景在单击时更改,并在单击另一个按钮时返回默认值。这样做的最佳方法是什么?

我可以在 when 语句中为每个案例调用多个操作吗?我需要为每个单击的按钮设置其背景和其他 7 个的背景吗?看起来代码太多了。

class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onClick(v: View?) {

            when (v?.id){
                R.id.recipeOneButton -> createRecipeOne() 
                R.id.recipeTwoButton -> createRecipeTwo()
                R.id.recipeThreeButton -> createRecipeThree()
                R.id.recipeFourButton -> createRecipeFour()
                R.id.recipeFiveButton -> createRecipeFive()
                R.id.recipeSixButton -> createRecipeSix()
                R.id.recipeSevenButton -> createRecipeSeven()
                R.id.recipeEightButton -> createRecipeEight()

        }

    }

    private val manager = this.supportFragmentManager!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        createRecipeOne()

        val recipeOneButton : Button = findViewById(R.id.recipeOneButton)
        val recipeTwoFragment : Button = findViewById(R.id.recipeTwoButton)
        val recipeThreeButton : Button = findViewById(R.id.recipeThreeButton)
        val recipeFourButton : Button = findViewById(R.id.recipeFourButton)
        val recipeFiveButton : Button = findViewById(R.id.recipeFiveButton)
        val recipeSixButton : Button = findViewById(R.id.recipeSixButton)
        val recipeSevenButton: Button = findViewById(R.id.recipeSevenButton)
        val recipeEightButton : Button = findViewById(R.id.recipeEightButton)

        recipeOneButton.setOnClickListener(this)
        recipeTwoFragment.setOnClickListener(this)
        recipeThreeButton.setOnClickListener(this)
        recipeFourButton.setOnClickListener(this)
        recipeFiveButton.setOnClickListener(this)
        recipeSixButton.setOnClickListener(this)
        recipeSevenButton.setOnClickListener(this)
        recipeEightButton.setOnClickListener(this)

    }


    fun createRecipeOne(){
        val transaction = manager.beginTransaction()
        val fragment = RecipeOne()
        transaction.replace(R.id.fragmentHolder, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createRecipeTwo(){
        val transaction = manager.beginTransaction()
        val fragment = RecipeTwo()
        transaction.replace(R.id.fragmentHolder, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createRecipeThree(){
        val transaction = manager.beginTransaction()
        val fragment = RecipeThree()
        transaction.replace(R.id.fragmentHolder, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createRecipeFour(){
        val transaction = manager.beginTransaction()
        val fragment = RecipeFour()
        transaction.replace(R.id.fragmentHolder, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createRecipeFive(){
        val transaction = manager.beginTransaction()
        val fragment = RecipeFive()
        transaction.replace(R.id.fragmentHolder, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createRecipeSix(){
        val transaction = manager.beginTransaction()
        val fragment = RecipeSix()
        transaction.replace(R.id.fragmentHolder, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createRecipeSeven(){
        val transaction = manager.beginTransaction()
        val fragment = RecipeSeven()
        transaction.replace(R.id.fragmentHolder, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

    fun createRecipeEight(){
        val transaction = manager.beginTransaction()
        val fragment = RecipeEight()
        transaction.replace(R.id.fragmentHolder, fragment)
        transaction.addToBackStack(null)
        transaction.commit()
    }

}

标签: androidkotlin

解决方案


你可以做这样的事情when来改变那个按钮的背景

when (v?.id){
            R.id.recipeOneButton -> {
              createRecipeOne() 
              changebackground(id)
              }
            R.id.recipeTwoButton -> {
              createRecipeTwo()
              changebackground(id)
               }
  }

changebackground(id)传递该按钮的 id 以供您更改背景。

用于更改其他按钮背景。如果你想减少代码。你必须做这样的事情。

private void changeColor(Button[] buttons){
  for(int x=0; x < buttons.length; x++){

     buttons[x].setBackgroundColor(Color.RED);
   }
}

但为此,您必须像这样初始化按钮

btns[0] = (Button) findViewById(R.id.recipeOneButton );
btns[1] = (Button) findViewById(R.id.recipeTwoButton );

推荐阅读