【问题标题】:2 line list XML with check box带复选框的 2 行列表 XML
【发布时间】:2011-04-28 15:47:49
【问题描述】:

我有一个 2 行列表 XML,在第一行也有一个复选框。此数据不会保存,它只是在您执行列出的任务时检查列表中的项目。

我有两个问题。当我选中第一个复选框时,列表下方的另一行也会被选中。该复选框没有 ID,但必须有一些内容表明它是第一行的一部分或其他内容?

此外,当我更改方向时,屏幕会刷新,我想停止刷新,因为复选框未保存。

这里是 XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/BG" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:orientation="horizontal">

    <CheckBox android:text="" android:paddingRight="-10sp"
        android:layout_marginRight="-10sp" android:layout_weight=".1"
        android:textColor="@color/ndList" 
        android:layout_width="wrap_content" android:layout_height="wrap_content">

    </CheckBox>

    <TextView android:id="@+id/Ltext1" android:textColor="@color/ndList"
        android:layout_width="wrap_content" android:layout_weight="2"

        android:layout_height="wrap_content" android:layout_marginLeft="6dip"
        android:layout_marginTop="6dip" android:text="ONE"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:orientation="horizontal">

    <TextView android:id="@+id/Ltext2" android:textColor="@color/ndList"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_below="@+id/Ltext1" android:layout_alignLeft="@+id/Ltext1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="TWO" />
</LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android xml listview


    【解决方案1】:

    我没有完全遵循问题的第一部分,与复选框状态保存有关的第二部分回答如下:

    活动在方向更改时关闭并重新启动。

    使用onSaveInstanceState(Bundle) 存储所选选项并将该选择恢复到onCreate(Bundle)

    onSaveInstanceState(Bundle bundle) {
    bundle.putBoolean("checked", checkBox.isChecked());
    }
    
    
    onCreate(Bundle bundle) {
    if (bundle.containsKey("checked")) {
    checkBox.setChecked(bundle.getBoolean("checked"));
    }
    

    【讨论】:

      【解决方案2】:

      您应该确保,您更新了所有可以在您的 intemrenderer 中更改其内容的视图。这必须在您的ListAdaptergetView 方法中完成。
      这也包括复选框。因此,如果您没有保持复选框选中状态的基础数据成员(项目数据中的 boolean),复选框将继续清除其状态。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-09
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-14
        • 2017-10-15
        相关资源
        最近更新 更多