如何检查单选按钮android的状态变化? [英] How to check state Changing of Radio Button android?

查看:36
本文介绍了如何检查单选按钮android的状态变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查radioButton是否被点击改变一些值

i want to check if radioButton is clicked change some value

这是我的代码:

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

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

The first two radios are created without referencing but the last one is my Answer so i got a reference to check if is checked.

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

Here my Question : How can i check if the radioButton is ticked?

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

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

this is my Approach so far but it does not work perfectly because when users select radioButton and after that will change their choice the value of goToNextLevel does not change and will set to true in the wrong way.

如何找出只要选中radioButton 将goToNextLevel 设置为true,否则为false.

How to find out as long as the radioButton is checked set the goToNextLevel to true and otherwise to false.

推荐答案

首先创建 RadioGroup 如果你还没有这样做,然后像往常一样通过它的 id 然后使用 setOnCheckedChangeListener.您可以检查您的 RadioButton 是否被选中,也可以有多个 RadioButton.例如:

First create RadioGroup if you didn't do that yet, then as usual connect it by it's id and then use setOnCheckedChangeListener. You can check is your RadioButton checked or not also you can have more then one RadioButton. For example:

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;
            }
        }
    });

这篇关于如何检查单选按钮android的状态变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆