单选按钮和复选框
RadioButton and Checkbox
我有 3 个单选按钮和 10 个复选框,我只是想阻止用户选择任何复选框,直到它先选择一个单选按钮,请帮忙。
默认情况下将所有复选框设置为禁用。然后为所有单选按钮添加检查更改侦听器。添加代码以启用单选按钮检查更改侦听器上的所有复选框。
使用 RadioButtons 方法,将 Checkboxes 设置为禁用并检查单选按钮之一是否被选中(),如果为真,则将 checkboxex 设置为启用。
您可以选择一个单选按钮,默认情况下这样用户将不得不使用默认选择,或者将更改选择 - 这两种方式您始终选择一个单选按钮。
如果您不想保留默认的单选按钮,
使用广播组
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
禁用开头的所有复选框
checkBox.setEnabled(false);
为radio组设置onClick监听器并启用里面的checkBox。
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
checkBox.setEnabled(false);
}
});
在广播组内添加 RadioButton
。
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radio_group"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="btn 1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn 2"
android:id="@+id/button2"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="btn 3"
/>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/radio_group"
android:layout_marginTop="10dp"
android:orientation="vertical"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check1"
android:text="check 1"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check2"
android:text="check 2"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check3"
android:text="check 3"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check4"
android:text="check 4"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check5"
android:text="check 5"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check6"
android:text="check 6"
/>
</LinearLayout>
并在 activity 中实现 RadioGroup onCheckChangeListener。在此之前确保通过 checkbox.setEnabled(false)
禁用所有复选框
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton1,radioButton2,radioButton3;
CheckBox checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup= (RadioGroup) findViewById(R.id.radio_group);
radioButton1= (RadioButton) findViewById(R.id.button1);
radioButton2= (RadioButton) findViewById(R.id.button2);
radioButton3= (RadioButton) findViewById(R.id.button3);
checkBox1= (CheckBox)findViewById(R.id.check1);
checkBox2= (CheckBox)findViewById(R.id.check2);
checkBox3= (CheckBox)findViewById(R.id.check3);
checkBox4= (CheckBox)findViewById(R.id.check4);
checkBox5= (CheckBox)findViewById(R.id.check5);
checkBox6= (CheckBox)findViewById(R.id.check6);
checkBox1.setEnabled(false);
checkBox2.setEnabled(false);
checkBox3.setEnabled(false);
checkBox4.setEnabled(false);
checkBox5.setEnabled(false);
checkBox6.setEnabled(false);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked){
checkBox1.setEnabled(true);
checkBox2.setEnabled(true);
checkBox3.setEnabled(true);
checkBox4.setEnabled(true);
checkBox5.setEnabled(true);
checkBox6.setEnabled(true);
}
}
});
}
我有 3 个单选按钮和 10 个复选框,我只是想阻止用户选择任何复选框,直到它先选择一个单选按钮,请帮忙。
默认情况下将所有复选框设置为禁用。然后为所有单选按钮添加检查更改侦听器。添加代码以启用单选按钮检查更改侦听器上的所有复选框。
使用 RadioButtons 方法,将 Checkboxes 设置为禁用并检查单选按钮之一是否被选中(),如果为真,则将 checkboxex 设置为启用。
您可以选择一个单选按钮,默认情况下这样用户将不得不使用默认选择,或者将更改选择 - 这两种方式您始终选择一个单选按钮。
如果您不想保留默认的单选按钮,
使用广播组
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
禁用开头的所有复选框
checkBox.setEnabled(false);
为radio组设置onClick监听器并启用里面的checkBox。
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { checkBox.setEnabled(false); } });
在广播组内添加 RadioButton
。
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radio_group"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="btn 1"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn 2"
android:id="@+id/button2"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:text="btn 3"
/>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/radio_group"
android:layout_marginTop="10dp"
android:orientation="vertical"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check1"
android:text="check 1"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check2"
android:text="check 2"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check3"
android:text="check 3"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check4"
android:text="check 4"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check5"
android:text="check 5"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check6"
android:text="check 6"
/>
</LinearLayout>
并在 activity 中实现 RadioGroup onCheckChangeListener。在此之前确保通过 checkbox.setEnabled(false)
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton1,radioButton2,radioButton3;
CheckBox checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup= (RadioGroup) findViewById(R.id.radio_group);
radioButton1= (RadioButton) findViewById(R.id.button1);
radioButton2= (RadioButton) findViewById(R.id.button2);
radioButton3= (RadioButton) findViewById(R.id.button3);
checkBox1= (CheckBox)findViewById(R.id.check1);
checkBox2= (CheckBox)findViewById(R.id.check2);
checkBox3= (CheckBox)findViewById(R.id.check3);
checkBox4= (CheckBox)findViewById(R.id.check4);
checkBox5= (CheckBox)findViewById(R.id.check5);
checkBox6= (CheckBox)findViewById(R.id.check6);
checkBox1.setEnabled(false);
checkBox2.setEnabled(false);
checkBox3.setEnabled(false);
checkBox4.setEnabled(false);
checkBox5.setEnabled(false);
checkBox6.setEnabled(false);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
boolean isChecked = checkedRadioButton.isChecked();
if (isChecked){
checkBox1.setEnabled(true);
checkBox2.setEnabled(true);
checkBox3.setEnabled(true);
checkBox4.setEnabled(true);
checkBox5.setEnabled(true);
checkBox6.setEnabled(true);
}
}
});
}