【问题标题】:How to check state Changing of Radio Button android?如何检查单选按钮android的状态变化?
【发布时间】:2018-01-22 13:04:33
【问题描述】:

我想检查单选按钮是否被点击改变一些值

这是我的代码:

new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,"Ok");
new DynamicViews().makeRadioButton(getApplicationContext(),radioGroup,"Ok");
RadioButton radioButton =
                        new DynamicViews().makeRadioButtonforAnswer(getApplicationContext(),radioGroup,"This is the answer");

前两个收音机是在没有参考的情况下创建的,但最后一个是我的答案,所以我得到了一个参考来检查是否已检查。

这是我的问题: 我如何检查radioButton是否被勾选?

   radioButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        goToNextLevel = true;
                    }
                });

到目前为止,这是我的方法,但它不能完美地工作,因为当用户选择 radioButton 并且之后会改变他们的选择时,goToNextLevel 的值不会改变,并且会以错误的方式设置为 true。

只要选中单选按钮,如何找出将 goToNextLevel 设置为 true,否则设置为 false。

【问题讨论】:

    标签: android radio-button


    【解决方案1】:

    如果您还没有创建RadioGroup,则首先创建id,然后像往常一样通过id 连接它,然后使用setOnCheckedChangeListener。您可以检查您的RadioButton 是否已检查,也可以有多个RadioButton。例如:

    radioGroup.setOnCheckedChangeListener(new new OnCheckedChangeListener(){
        @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch(checkedId)
                {
                case R.id.firstRadioButton:
                    //Implement logic
                    break;
                case R.id.secondRadioButton:
                    //Implement logic
                    break;
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2011-08-02
      • 2013-09-20
      • 1970-01-01
      • 2011-07-29
      • 2018-07-23
      • 2012-12-15
      • 1970-01-01
      • 2011-04-28
      • 1970-01-01
      相关资源
      最近更新 更多