【问题标题】:Check one CheckBox set another uncheck in a ListView选中一个 CheckBox 在 ListView 中设置另一个取消选中
【发布时间】:2018-01-20 11:55:02
【问题描述】:

listView with dual CheckBox Image

Android 新手
我在一个列表中有两个复选框,我想一次检查一个,而另一个无法检查

我的 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/student_name"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:padding="20dp"/>

    <CheckBox
        android:id="@+id/present_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:duplicateParentState="false" />

    <CheckBox
        android:id="@+id/absent_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

list.xml

<LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/names_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>

    </LinearLayout

【问题讨论】:

  • 你想使用 RadioButtons,而不是。

标签: android listview checkbox


【解决方案1】:

您可以使用 RadioGroup 代替 Checkbox,然后将两个 radioButton 添加到 radioGroup。

【讨论】:

    【解决方案2】:

    出于您的目的,请使用RadioButton,检查此tutorial

    另外,请参阅此 answer,了解如何使用 RadioButtonRadioGroup 进行操作

    【讨论】:

      【解决方案3】:

      单选按钮专为这种情况而设计。但是如果你真的想使用复选框,当单击事件发生在复选框上的列表中的项目时,将另一个复选框设置为与当前复选框相反的状态。所以如果你收到这样的伪代码事件:

      onCheckChanged(CheckBox oneCheckbox, boolean isChecked){
          otherCheckbox.setChecked(!isCchecked);
      }
      

      【讨论】:

        【解决方案4】:

        当您点击listView 中的一行时,您必须调用notifyDataSetChanged(),以便更新所有视图。

        listView.setOnItemClickListener((parent, view, position, id) -> {
                
        // do something
            
        notifyDataSetChanged()
        });
        

        【讨论】:

          猜你喜欢
          • 2014-05-05
          • 2023-04-05
          • 1970-01-01
          • 2015-06-18
          • 1970-01-01
          • 2016-11-07
          • 1970-01-01
          • 1970-01-01
          • 2018-04-15
          相关资源
          最近更新 更多