【问题标题】:why the checkBox is check when setChecked(false) is called为什么在调用 setChecked(false) 时检查复选框
【发布时间】:2017-02-16 17:29:33
【问题描述】:

具有几个动态充气/添加的复选框,当有些是检查旋转或最小化应用程序(为了易于触发这种情况,打开'不保留活动活动'),还原的UI视图显示了检查的所有复选框。

当操作系统执行 saveInstance 时,我们将选中的项目存储在一个列表中,当操作系统恢复片段时,我们得到选中项目的列表,当重新填充复选框行时,它调用 setChecked(true) 或 setChecked(false ) 基于列表。 但是在那之后所有的复选框都显示为选中,虽然在调试中它清楚地显示只有选中的那些被使用'true'而其他的被使用'false'和setChecked()。

任何人都经历过同样的事情,或者知道为什么单个 checkBox 实例上的 setChecked() 不执行所谓的操作吗?

itemList = [A, B, C, D, E]

checkedListSet = [A, B]

void insertOneRow(ViewGroup container, Item item) {

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemLayout = inflater.inflate(R.layout.item_row, null, false);

    TextView txt = (TextView)itemLayout.findViewById(R.id.text);
    txt.setText(item.toString());        
    container.addView(itemLayout, container.getChildCount());

    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.checkbox);
    if (checkbox != null) {
        boolean isChecked = (checkedListSet.get(item) != null);

        Log.i(“insertOneRow(), isChecked:"+ isChecked +", item:”+item);

        checkbox.setChecked(isChecked);  //<== trace shows only A and B are set with true
    }
}

item_row.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="36dp"
    android:orientation="horizontal"
>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:checked="false"            />

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"             
   />

</LinearLayout>

【问题讨论】:

  • checkedListSet 对象的类型是什么?我认为列表上的 get 方法只接受一个 int 作为索引。
  • checkedList 只是一个包含被检查项的列表,可以是 ListArray 或 HashSet,checkedListSet.get(item) != null 只是想说该项目是否存在在 checkList 中,checkBox 需要设置为 true。

标签: android checkbox onsaveinstancestate


【解决方案1】:

检查所有复选框的原因是它们都具有相同的id。如果其中任何一个被选中并被重新创建(由于方向更改或重新附加片段),Android 系统会尝试恢复它们的状态并根据它们的 id 识别它们。因此,所有这些都得到了检查。

android:saveEnabled 标志告诉 Android 系统是否保存其状态并尝试稍后恢复。

由于您已经有一种机制可以将其状态设置 android:saveEnabled 恢复为 false 适合您。

查看这个问题,EditText 会发生类似的事情:Why does Android change the value of EditTexts with same id?

【讨论】:

    【解决方案2】:
    checkedListSet.get(item) != null
    

    如果您的列表中有一个项目并且它不为空,则此方法将返回 true。

    您正在将此表达式的结果分配给您的 isChecked 布尔值。

    因此,每次该表达式返回 true 时,您的复选框都会被 setChecked true。

    在设置复选框之前,我不知道您要检查的确切条件,所以我无法帮助您。

    【讨论】:

    • 见我上面的评论。这不是真正的运行代码,它只是尝试说如果项目在checkedList中,那么checkBox将被设置为true。
    【解决方案3】:

    仍然不知道为什么,但是在输入 android:saveEnabled="false" 后,相同的代码开始工作。有谁知道为什么它必须有 android:saveEnabled="false"?

    <CheckBox
            android:saveEnabled="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 2020-02-27
      相关资源
      最近更新 更多