【问题标题】:How to make single checked of checkbox in a Dialog Android?如何在 Dialog Android 中单独选中复选框?
【发布时间】:2017-04-06 04:49:42
【问题描述】:

我在对话框中有两个复选框。我想为这些复选框设置单项检查。这意味着如果我选中了复选框 1,则复选框 2 将被取消选中。当我选中复选框 2 时类似。check_value 变量在选中复选框 1 时设置为 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 文件。 checkbox1 默认为 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>

【问题讨论】:

  • 为什么选择复选框而不是单选按钮?他们为此而生
  • 你能用单选按钮回答我的问题吗?请注意,我将它们放在对话框中
  • 对不起,现在我没有编译器。只需看看This post on how to use custom layouts in builders,您也可以参考this link also 了解如何使用单选按钮。希望这会有所帮助

标签: android checkbox android-widget


【解决方案1】:

试试这个:

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

    }
});}

【讨论】:

  • 等等。你细化 check_value 的设置是否正确?
  • 你用check_value做什么?
  • 如果 checkbox1 被选中,我想要 check_value=true,如果 checkbox2 被选中则等于 false
【解决方案2】:

您应该使用带有 2 个单选按钮的单选组。这具有您正在寻找的默认功能。复选框不适用于此。

【讨论】:

    【解决方案3】:

    在Android中,你可以使用“android.widget.CheckBox”类来渲染一个复选框。

    在本教程中,我们将向您展示如何在 XML 文件中创建 3 个复选框,并演示使用侦听器来检查复选框状态 - 选中或未选中。

    https://www.mkyong.com/android/android-checkbox-example/

    【讨论】:

      【解决方案4】:

      喜欢下面的代码...

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

      然后在活动中做...

      这里的 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;
                  }
              }
          });
      

      【讨论】:

      • R.id.rbTrue 在 DialogView 中不起作用。我以前用过
      猜你喜欢
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 2020-06-03
      • 2021-11-15
      • 1970-01-01
      • 2015-07-11
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多