动态添加单选按钮时单选组中的问题 [英] Issue in radiogroup while adding radiobuttons dynamically

查看:120
本文介绍了动态添加单选按钮时单选组中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到与我的问题类似的任何帖子。我有一个 radiogroup ,它具有两个如下按钮,

I didn't see any post similar to my problem. I have a radiogroup which has two buttons as follows,

<RadioGroup
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioButton
            android:id="@+id/basic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:id="@+id/advanced"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    </RadioGroup>

在我的应用程序中,有时我需要我已动态添加的另一个按钮例如,

In my application,Sometimes I need one more button which I have added dynamically like,

button=new AppCompatRadioButton(Activity.this);                                                              
button.setId(R.id.my_custom_id);                                                                                  
radiogroup.addView(button);

该无线电组的Oncheckedchangelistener代码,

Oncheckedchangelistener code for that radiogroup,

(radioGroup).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    if(checkedId== R.id.basic) {
                        button.setChecked(false);
                        //my code
                    }
                    else if(checkedId== R.id.advanced) {
                        button.setChecked(false);
                        //my code
                    }
                    else {
                       //it is newly added button's part
                    }
                }
            });




问题是,当选中新添加的按钮时,它是当我单击其他按钮时,未选中
。我试图通过设置 button.setChecked(false);
OnCheckedChangeListener 中解决此问题b检查了其他按钮。但这不起作用。我不知道是哪个造成了
的问题。

The problem is, when the newly added button is checked, it is not unchecked when I am clicking other button. I tried to solve this in OnCheckedChangeListener by setting button.setChecked(false); when other buttons were checked. But it doesn't work. I don't know which makes problem in that.

任何人都可以帮助我。谢谢!

Can Anyone help me.Thanks!

推荐答案

在初始化RadioButton时使用Activity类上下文。

Use your Activity class context while initializing the RadioButton.

代替 Activity.this 使用 MainActivity.this

这是您的活动中的完整工作代码。.

Here is the Full working code..in your activity:

        RadioGroup group = (RadioGroup) findViewById(R.id.group);

        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                Log.d("chk", "id" + checkedId);

                if (checkedId == R.id.basic) {
                    //some code
                    Toast.makeText(MainActivity.this, "First", Toast.LENGTH_SHORT).show();
                } else if (checkedId == R.id.advanced) {
                    //some code
                    Toast.makeText(MainActivity.this, "Second", Toast.LENGTH_SHORT).show();
                } else if (checkedId == R.id.my_custom_id) {
                    //some code
                    Toast.makeText(MainActivity.this, "Third", Toast.LENGTH_SHORT).show();
                }

            }

        });
        RadioButton button = new RadioButton(this);
        button.setId(R.id.my_custom_id);
        button.setText("Dynamic Button");
        group.addView(button);
        group.clearCheck();

在您的布局Xml中:

<RadioGroup
    android:id = "@+id/group"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <RadioButton
        android:id="@+id/basic"
        android:layout_width="wrap_content"
        android:text="Basic"
        android:layout_height="wrap_content"
        />
    <RadioButton
        android:id="@+id/advanced"
        android:layout_width="wrap_content"
        android:text="Advanced"
        android:layout_height="wrap_content"
        />
</RadioGroup>

这篇关于动态添加单选按钮时单选组中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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