【问题标题】:Android Checkbox doesn't return right valueAndroid Checkbox 没有返回正确的值
【发布时间】:2018-04-22 08:46:09
【问题描述】:

我正在使用带有复选框的自定义列表视图。我想知道复选框是否被选中。所以我使用getCheckedItemPositions() 来获取checked_info。如果我设置 "android:clickable = "true" ",复选框不会返回结果。但是,如果我设置“android:clickable = “false””,视图不会发生任何变化,但复选框会返回其选中的信息。什么是问题?底部代码是我使用 getCheckedItemPostions() 和自定义 listiview 的 XML 代码的方法。

public void deleteButton(View v)
{
    int i,count;
    count = mMyAdapter.mItems.size();
    SparseBooleanArray checked = mListView.getCheckedItemPositions();
}





<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/checkBox1"
            android:visibility="gone"
            android:focusable="false"
            android:clickable="false"
            android:checkable="true"



            android:layout_weight="1" />


        <LinearLayout

            android:layout_weight="4"

            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <TextView
                android:layout_weight="1"
                android:id="@+id/name"
                android:layout_width="15dp"
                android:layout_height="match_parent"
                android:textSize="10pt"
                android:text="name.." />


            <TextView
                android:layout_weight="1"
                android:id="@+id/time"
                android:layout_width="15dp"
                android:layout_height="match_parent"
                android:textSize="12pt"
                android:text="name.." />


        </LinearLayout>


        <ToggleButton
            android:layout_weight="1"
            android:layout_gravity="right"
            android:id="@+id/toggleButton3"
            android:layout_width="6dp"
            android:layout_height="match_parent"

            android:text="Toggle"
            android:textOff="Off"
            android:textOn="On" />

    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android xml checkbox


    【解决方案1】:

    参考此“https://www.tutorialspoint.com/android/android_checkbox_control.htm”检查一次。如果你没有得到答复通知。

    【讨论】:

      【解决方案2】:

      您需要实现 setOnCheckedChangeListener ,因为通过设置可点击的 false 将停止执行周期,一旦单击它之后您将无法获得更新的值,因此将其设置为 true 但实现 setOnCheckedChangeListener 以通过创建布尔值将复选框的状态保存为选中或不选中每个复选框的变量。然后它将起作用。

      按照本教程实现 onCheckedChangeListener:

      http://www.technotalkative.com/android-checkbox-example/

      【讨论】:

      • listiview中的checkbox如何实现setOnCCheckedChangeListener?
      • 另外,如果它对你有用,别忘了将我的帖子标记为 answer.Happy Coding bro :)
      猜你喜欢
      • 2016-04-21
      • 2014-11-26
      • 2019-03-21
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多