1

我有 2 个单选组,每个单选组包含 2 个单选按钮。我试图在这两组之间切换,但到目前为止没有成功。任何帮助,将不胜感激。


        myGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
        myGroup3 = (RadioGroup) findViewById(R.id.radioGroup3);
        myGroup3.clearCheck();
        myGroup2.clearCheck();

        myGroup2.setOnCheckedChangeListener(this);
        myGroup3.setOnCheckedChangeListener(this);


myGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            // @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                    myGroup3.clearCheck();
            }
        });
    myGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                    myGroup2.clearCheck();
            }
        });
4

2 回答 2

3

在此处检查 API:

http://developer.android.com/reference/android/widget/RadioGroup.OnCheckedChangeListener.html

编辑:

不高兴花了多长时间,但我想通了。结果 onCheckedChanged 仅在组已清除时为 checkedId 传递 -1。如果一个框刚刚被取消选中,它的 ID 仍然被传递。

你可以这样做:

    myGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId != -1 &&
                ((RadioButton) findViewById(checkedId)).isChecked()){
                myGroup3.clearCheck();
            }
        }
    });
    myGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId != -1 &&
                        ((RadioButton) findViewById(checkedId)).isChecked()){
                    myGroup2.clearCheck();
                }
        }
    });

或者,可能更明智一点,让活动实现侦听器,这部分在 onCreate 方法之外:

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    if(checkedId != -1 && ((RadioButton) findViewById(checkedId)).isChecked()){
        if(group == myGroup2){
            myGroup3.clearCheck();
        }
        else if(group == myGroup3){
            myGroup2.clearCheck();
        }
        // And your switch for whatever you want to happen based on the
        // selected button goes here
    }
}
于 2013-02-05T03:20:01.740 回答
1

这是我想出的解决方案。

radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
    radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
    RadioButton[] tempradbutton1 = {
            (RadioButton) findViewById(R.id.radio1),
            (RadioButton) findViewById(R.id.radio2) };
    RadioButton[] tempradbutton2 = {
            (RadioButton) findViewById(R.id.radio4),
            (RadioButton) findViewById(R.id.radio5) };
    radbutton1 = tempradbutton1;
    radbutton2 = tempradbutton2;
    for (int i = 0; i < radbutton1.length; i++) {
        radbutton1[i].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                radioGroup2.clearCheck();
            }
        });
        radbutton2[i].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                radioGroup1.clearCheck();

            }
        });
    }

与其使用setOnCheckedChangeListener,不如对 radiogroup 上存在的每个单选按钮使用onClickListener

XML:

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />
</RadioGroup>

<RadioGroup
    android:id="@+id/radioGroup2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/radioGroup1" >
    <RadioButton
        android:id="@+id/radio4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radio5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RadioButton" />
</RadioGroup>
于 2013-02-05T03:55:28.027 回答