samedi 23 mai 2020

Button doesn't set to Enabled even after "if conditions" are met

Below code is supposed to set "isEnabled" attribute of a button to true, but it doesn't. I initialize a mutable list which adds a String when certain Switches are on, and remove them when are off.

I created an if condition where if the size of the list is equal to 2 then ok_button is enabled. I can't see why the ok_button is not updated even when the conditions are met.

package com.example.malakes
import android.nfc.Tag
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.CompoundButton
import android.widget.Switch
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import android.util.Log
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity() {

companion object{ const val TAG = "MyActivity" } //define TAG

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

    val epilegmenoi: MutableList<String> = mutableListOf()

    val tony = findViewById<Switch>(R.id.switchTony)
    val giorgis = findViewById<Switch>(R.id.switchGiorgos)
    val duke = findViewById<Switch>(R.id.switchDuke)
    val nikolas = findViewById<Switch>(R.id.switchNikolas)
    val dionisis = findViewById<Switch>(R.id.switchDionisis)
    val grigoris = findViewById<Switch>(R.id.switchGrigoris)
    val ok_button = findViewById<Button>(R.id.buttonOK)
    val clear_button = findViewById<Button>(R.id.buttonCLEAR)

    tony.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked) {
            epilegmenoi.add("Tony")
        } else {
            epilegmenoi.remove("Tony")
        }


    }
    giorgis.setOnCheckedChangeListener { _, isChecked ->
        if (isChecked) {
            epilegmenoi.add("Giorgis")
        } else {
            epilegmenoi.remove("Giorgis")
        }


    }

    if (epilegmenoi.size == 2) {ok_button.isEnabled=true}

}

}

Aucun commentaire:

Enregistrer un commentaire