0

假设我有 2 个复选框(“checkbox1”和“checkbox2”)并且我设置:SetChecked(true) 用于“checkbox1”和:SetChcked(false) 用于“checkbox2”。我的目的是在我单击其中一个时关联它们并在选中状态之间切换(如果选中“checkbox1”并且我选中了“checkbox2”,则将选中“checkbox2”并且将取消选中“checkbox1”,反之亦然)
这是我的代码:

final CheckBox ChckBoxNo = (CheckBox) promptsView.findViewById(R.id.ChkBoxNo);
final CheckBox ChckBoxYes = (CheckBox) promptsView.findViewById(R.id.ChkBoxYes);
ChckBoxNo.setChecked(true);
ChckBoxYes.setChecked(false);

ChckBoxNo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (ChckBoxNo.isChecked()) {
            ChckBoxYes.setChecked(false);
        } else if (!ChckBoxNo.isChecked()) {
            ChckBoxYes.setChecked(true);
        }

    }

});

ChckBoxYes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (ChckBoxYes.isChecked()) {
            ChckBoxNo.setChecked(false);
        } else if (!ChckBoxYes.isChecked()) {
            ChckBoxNo.setChecked(true);
        }

    }

});

   //these if's allways response to "ChckBoxNo.IsChecked()" ,even if its the 
     // opposite and "ChckBoxYes.isChcked()"          


   if(ChckBoxYes.isChecked())
   {do ...
         }
    else if (ChckBoxNo.isChecked()
      { do ...
               }

当我通过能见度运行应用程序时,一切都很好), ChckBoxYes.SetChecked(false) ,即使它的相反并且“ChckBoxYes”被检查了)我应该怎么做才能解决这个问题?谢谢 !

4

1 回答 1

1

这不是解决问题的正确方法。幸运的是,已经有一个名为 RadioGroup 的内置组件。你可以这样做:

让您的 Activity 的 xml 文件如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radio_button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CheckBox1" />

        <RadioButton
            android:id="@+id/radio_button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="CheckBox2" />
    </RadioGroup>
</LinearLayout>

在您的 Activity 中,您可以与该组件进行交互,并在点击任何 RadioButtons 时处理您想要的任何内容,如下所示:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_button1) {
                    //First is checked
                } else if (checkedId == R.id.radio_button2) {
                    // Second is checked
                }
            }
        });
    }
}
于 2016-01-14T22:09:28.737 回答