如何在对话框 Android 中勾选复选框?
How to make single checked of checkbox in a Dialog Android?
我在对话框中有两个复选框。我想为这些复选框设置单选。这意味着如果我选中复选框 1,则复选框 2 将被取消选中。当我选中复选框 2 时类似。如果选中复选框 1,则 check_value
变量设置为 true;选中复选框 2 时,变量设置为 false。
这是我的代码,但无法达到我的预期
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialogView, null);
boolean check_value=false;
checkbox1 = (CheckBox) dialogView.findViewById(R.id.checkbox1);
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkbox2.setChecked(false);
checkbox1.setChecked(true);
check_value=true;
}
});
checkbox2 = (CheckBox) dialogView.findViewById(R.id.checkbox2);
checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkbox2.setChecked(true);
checkbox1.setChecked(false);
check_value=false;
}
});
}
这是我的 xml 文件。复选框 1 默认为 true
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/list"
android:layout_centerInParent="true">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
android:checked="true"
android:layout_marginLeft="2dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox2"
android:checked="false"
android:layout_marginLeft="2dp" />
</LinearLayout>
您应该使用带有 2 个单选按钮的单选组。这具有您正在寻找的默认功能。复选框不适用于此。
在Android中,您可以使用“android.widget.CheckBox”class来呈现复选框。
在本教程中,我们向您展示了如何在 XML 文件中创建 3 个复选框,并演示了如何使用侦听器来检查复选框状态——选中或未选中。
试试这个:
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialogView, null);
boolean check_value=false;
checkbox1 = (CheckBox) dialogView.findViewById(R.id.checkbox1);
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checkbox2.setChecked(false);
check_value=true;
}
else
checkbox2.setChecked(true);
}
});
checkbox2 = (CheckBox) dialogView.findViewById(R.id.checkbox2);
checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checkbox1.setChecked(false);
check_value=false;
}
else
checkbox1.setChecked(true);
}
});}
喜欢下面的代码...
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbTrue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Accept"/>
<RadioButton
android:id="@+id/rbFalse"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Reject"/>
</RadioGroup>
然后在 activity 喜欢...
这里radioGroup是RadioGroup的对象,由单选按钮组成
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.rbTrue){
check_value = true;
}else{
check_value = false;
}
}
});
我在对话框中有两个复选框。我想为这些复选框设置单选。这意味着如果我选中复选框 1,则复选框 2 将被取消选中。当我选中复选框 2 时类似。如果选中复选框 1,则 check_value
变量设置为 true;选中复选框 2 时,变量设置为 false。
这是我的代码,但无法达到我的预期
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialogView, null);
boolean check_value=false;
checkbox1 = (CheckBox) dialogView.findViewById(R.id.checkbox1);
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkbox2.setChecked(false);
checkbox1.setChecked(true);
check_value=true;
}
});
checkbox2 = (CheckBox) dialogView.findViewById(R.id.checkbox2);
checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkbox2.setChecked(true);
checkbox1.setChecked(false);
check_value=false;
}
});
}
这是我的 xml 文件。复选框 1 默认为 true
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/list"
android:layout_centerInParent="true">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
android:checked="true"
android:layout_marginLeft="2dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox2"
android:checked="false"
android:layout_marginLeft="2dp" />
</LinearLayout>
您应该使用带有 2 个单选按钮的单选组。这具有您正在寻找的默认功能。复选框不适用于此。
在Android中,您可以使用“android.widget.CheckBox”class来呈现复选框。
在本教程中,我们向您展示了如何在 XML 文件中创建 3 个复选框,并演示了如何使用侦听器来检查复选框状态——选中或未选中。
试试这个:
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_dialogView, null);
boolean check_value=false;
checkbox1 = (CheckBox) dialogView.findViewById(R.id.checkbox1);
checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checkbox2.setChecked(false);
check_value=true;
}
else
checkbox2.setChecked(true);
}
});
checkbox2 = (CheckBox) dialogView.findViewById(R.id.checkbox2);
checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
checkbox1.setChecked(false);
check_value=false;
}
else
checkbox1.setChecked(true);
}
});}
喜欢下面的代码...
<RadioGroup
android:id="@+id/myRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbTrue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Accept"/>
<RadioButton
android:id="@+id/rbFalse"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Reject"/>
</RadioGroup>
然后在 activity 喜欢...
这里radioGroup是RadioGroup的对象,由单选按钮组成
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.rbTrue){
check_value = true;
}else{
check_value = false;
}
}
});