Dev*_*per 2 android android-layout android-checkbox
我的布局中有一个清除按钮,单击该按钮我想取消选中线性布局内的所有复选框listview。
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id ="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:gravity="right|center"
android:text="Remove" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/listview"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:cacheColorHint="@android:color/transparent"
android:divider="#fff"
android:dividerHeight="1dp"
android:fadingEdge="none">
</ListView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在里面,listview我正在膨胀一个带有复选框的布局。现在btnClear我想取消选中所有复选框。我们该怎么做,请帮助我
创建一个 for 循环,遍历 的所有子项ListView,并检查 是否View为 CheckBox 类型并setChecked(false)对其执行。
for (int i = 0; i < mListView.getCount(); i++) {
View mChild = mListView.getChildAt(i);
//Replace R.id.checkbox with the id of CheckBox in your layout
CheckBox mCheckBox = (CheckBox) mChild.findViewById(R.id.checkbox);
mCheckBox.setChecked(false);
}
Run Code Online (Sandbox Code Playgroud)