java - Android - 如何使选中一个复选框取消选中另一个?

标签 java android

我有两个复选框。如果选中一个,我希望取消选中另一个。

代码:

 CheckBox ozCheckBox = (CheckBox)findViewById(R.id.oz);
    boolean ozInput = ozCheckBox.isChecked();
    CheckBox gCheckBox = (CheckBox)findViewById(R.id.g);
    boolean gInput = gCheckBox.isChecked();

最佳答案

为什么不使用单选按钮和单选组来实现此目的? 这可以使用 RadioGroup 中的两个 RadioButton 来完成。您还可以将 RadioButton 的样式设置为与 CheckBox 完全相同。

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

            <RadioButton
                android:id="@+id/one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="?android:attr/listChoiceIndicatorMultiple"
                android:text="CheckBox One" />

            <RadioButton
                android:id="@+id/two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="?android:attr/listChoiceIndicatorMultiple"
                android:text="CheckBox Two" />
        </RadioGroup>

关于java - Android - 如何使选中一个复选框取消选中另一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34699919/

相关文章:

android - Material 设计日期选择器 : How to popup datepicker when click on a Spinner

android - 使用 Facebook 凭据记录我的应用程序

android - 如何在 Android 中为不同的内容使用相同的 Activity?

java - 为什么我得到 org.hibernate.HibernateException : IOException occurred reading a binary value

java - Android SQLITE,无法仅在 Marshmallow 中打开数据库

java - 如何判断 Facebook 是否首次登录 - Android

java - 为了捕获 BOOT_COMPLETED,只需在 `AndroidManifest.xml` 中注册 BroadcastReceiver 就足够了吗?

java - 使用字符串数组中的条目作为创建新数组 Java 的名称

java - 如果 Kerberos 身份验证失败,它会始终回退到 NTLM 吗?

java - 如何使用 Delphi 提取 jar 文件中定义的符号?