简体   繁体   English

如何以编程方式检查动态广播组中的单选按钮?

[英]How to check a radio button programmatically in dynamic radio group?

I have a list of radio buttons. 我有一个单选按钮列表。 I want one to be selected when the application first launch. 我希望在应用程序首次启动时被选中。

Then when I will select some other radio button from the radio group that will be deselected. 然后,当我从单选按钮组中选择其他单选按钮时,该单选按钮将被取消选择。

Here's my code: 这是我的代码:

for (x in taskTypes) {
        var radioButton = RadioButton(context)
        radioButton.setOnCheckedChangeListener { button: CompoundButton?, b: Boolean  ->
            if (b) {
                ......

            }
        }

        taskRadioGroup.addView(radioButton)
    }

If you use radioGroup , previously selected radioButton should be automatically deselected. 如果您使用radioGroup ,则应自动取消选择先前选择的radioButton If you want a particular radioButton to be selected when activity is launched you have just have to add the following line in onCreate() : 如果要在启动活动时选择特定的radioButton ,则只需在onCreate()添加以下行:

yourDesiredRadioButton.isChecked = true;

You can use setTag(); 您可以使用setTag(); method to provide a unique identification for all the buttons in your group and check/change the properties by checking their tags using getTag() 为组中所有按钮提供唯一标识并通过使用getTag()检查其标签来检查/更改属性的方法

If you want to select the first RadioButton to be selected you can do this: 如果要选择要选择的第一个RadioButton ,可以执行以下操作:

var isChecked = true;

for (x in taskTypes) {
    var radioButton = RadioButton(context)

    if(isChecked){
        isChecked = false;
        radioButton.checked = true;
     }

    taskRadioGroup.addView(radioButton)
}

 taskRadioGroup.setOnCheckedChangeListener { button: CompoundButton?, b: Boolean  ->
        if (b) {
            ......

        }
    }

I want one to be selected when the application first launch 我希望在应用程序首次启动时被选中

On create method 关于创建方法

 hardradioButton!!.isChecked = true

make listener of your radioGroup On create method 使radioGroup的侦听器在create方法上

 radioGroup!!.setOnCheckedChangeListener(this)

implement OnCheckedChangeListener 实现OnCheckedChangeListener

override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
        when (checkedId) {
            R.id.hard_radiobuttonm -> {
                //do whatever
            }

            R.id.ok_radiobuttonm -> {
               //do whatever
            }

            R.id.easy_radiobuttonm -> {
               //do whatever
            }


            else -> {
            }
        }

    }

I'm not sure why it always check the first RadioButton even when ALL radio is added inside a RadioGroup. 我不确定为什么即使在RadioGroup内添加了所有单选按钮时也总是check第一个RadioButton。 My test also confirm your bug (maybe not bug, I just not sure why). 我的测试还确认了您的错误(也许不是错误,我只是不确定为什么)。

Here's my solution to your problem: 这是我为您解决的问题的解决方案:

var radioGroup = RadioGroup(applicationContext)

for (i: Int in 0..4){
    var radio = RadioButton(applicationContext)
    radio.setText("button position ${i+1}")

    radioGroup.addView(radio)
    Log.d("LOG", "Working on radio ${i+1}")
}

radioGroup.check(0)

linearParent.addView(radioGroup)

Noticed how I set the first radio by executing check(0) of the radioGroup . 注意了我如何通过执行radioGroup check(0)设置第一个无线电。 This is because whenever I set if from the radio itself, that radio will always be in checked state: 这是因为每当我从radio本身设置时,该收音机将始终处于checked状态:

//this code I placed inside for loop is somewhat broken...
if(i == 0){
    radio.isChecked = true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM