Issue in radiogroup while adding radiobuttons dynamically

倾然丶 夕夏残阳落幕 提交于 2019-12-24 06:23:10

问题


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

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!


回答1:


Use your Activity class context while initializing the RadioButton.

Instead of Activity.this use 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();

In your layout 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>



回答2:


Instead of making radio button programmatically through java code, do this, i.e. put that radio button in xml file, but initially set android:visibility to gone, this will make sure your last radio button is not visible :

<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"
        />

    <RadioButton
        android:id="@+id/new_radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility:"gone"
        />
</RadioGroup>

and, when you need to use your radio button, then in java class, make your radio button visible again, like this:

button.setVisibility(View.VISIBLE);

and then, you are good to go with setOnCheckedChangeListener :

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {
                    //your code
            });


来源:https://stackoverflow.com/questions/47471915/issue-in-radiogroup-while-adding-radiobuttons-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!