samedi 5 janvier 2019

Android/Kotlin - How to loop all the ToogleButtons to make a condition of if/else

I have the following group of toogleButtons (the green one is checked, and the other ones are unchecked) in an adapter:

enter image description here

I'm trying to find a solution to not do this in all the toogleButtons:

           holder.eleven.setOnCheckedChangeListener { buttonView, isChecked ->
                if(isChecked == true){
                    holder.eleven_dor.setTextColor(Color.WHITE)
                } else {
                    holder.eleven_dor.setTextColor(Color.BLACK)
                }
            }

I want to have a if/else statement which will do the previous work (set text color) for all the toogleButtons.

I've tried to specify an array to run in an loop but did not work:

           val array = arrayOf(R.id.one,
                    R.id.two,
                    R.id.three,
                    R.id.four,
                    R.id.five,
                    R.id.six,
                    R.id.seven,
                    R.id.eight,
                    R.id.nine,
                    R.id.ten,
                    R.id.eleven)
            for (i in array.indices) {
                array[i].setOnCheckedChangeListener { _, isChecked ->
                    if (isChecked) {
                        array[i].setTextColor(Color.WHITE)
                    } else {
                        array[i].setTextColor(Color.BLACK)
                    }
                }
             }

Do you know how I can make this better? Thanks.

Aucun commentaire:

Enregistrer un commentaire