【问题标题】:Checkbox value is true but tick is not showing - Android / Java复选框值为真,但未显示勾选 - Android / Java
【发布时间】:2023-03-19 19:58:01
【问题描述】:

基本上,我在三 (3) 个不同的片段中有三 (3) 个复选框列表。当我打开活动时,默认片段中的复选框正常显示,但不同片段中的复选框显示不同。复选框的值仍然为真。下面是复选框的图像,使事情更清楚,也是我的代码。

第一个片段页面:

第二个片段页面:

设置复选框的代码及其if condition

private void setCheckBox(ArrayList<DataPrefType> arrayList){
    for(int i = 0; i < arrayList.size(); i++){
        id = i + 1;
        checkBox = new CheckBox(getActivity());
        checkBox.setId(i + 1);
        checkBox.setText(arrayList.get(i).getName());
        checkBox.setPadding(10, 10, 10, 10);
        checkBox.setLayoutParams(params);
        checkBox.setGravity(Gravity.CENTER);
        checkBoxLayout.addView(checkBox);

        if(!userPreference.isEmpty() || userPreference != null){
            for(int j = 0; j < userPreference.get(0).getDataPrefTypeArrayList().size(); j++){
                int retrieveId = Integer.parseInt(userPreference.get(0).getDataPrefTypeArrayList().get(j).getId());
                if(checkBox.getId() == retrieveId)
                    checkBox.setChecked(true);
            }
        }

【问题讨论】:

  • 不是答案,而是提示...在您的第一个 if 语句中,您应该首先检查 userPreference 是否为空(在 !userPreference.isEmpty() 之前)并将 || 替换为 &amp;&amp; 所以两个条件都必须为真
  • 好的。谢谢你的提示:)
  • 我面临着完全相同的问题。我在选项卡式活动中有 2 个不同的片段,每个片段都有复选框。我将数据加载到基于 OnViewCreated 的复选框中。在第二个片段中,真正的复选框被突出显示而不是勾选。由于选项卡式活动,我觉得这是一个问题。
  • 我可以确认问题总是从第二个选项卡开始出现。我切换了标签位置,但问题仍然存在于第二个标签上。

标签: java android android-fragments checkbox


【解决方案1】:

即使 isChecked 为 true,复选框也未显示为已选中的错误出现在绘图动画中。

我找到了这个解决方案here。我将其发布在这里作为答案,以便更容易找到并包含有关如何调用它的一些额外信息。

尝试在根视图(或复选框的父视图)上调用 jumpDrawablesToCurrentState。如果像 OP 一样专门调用 checkBox.setChecked(true);,请在之后调用它。在这个问题中,在所有复选框都添加到父视图之后调用它是很重要的。这实际上将绘制所有处于当前状态的可绘制对象,跳过动画过程。

例子:

@Override
public void onStart() {
    View v = getView();
    if(v != null) {
        CheckBox chkYourCheckBox = (CheckBox)v.findViewById(R.id.chkYourCheckBox);
        chkYourCheckBox.setChecked(true); //sometimes this may draw fine, and other times it may not
        //***The important line:
        v.jumpDrawablesToCurrentState();
    }
}

【讨论】:

  • 感谢您的解决方案,它很棒
  • 当我在 onStart() 而不是 onCreateView() 创建动态视图时,它也适用于我
【解决方案2】:

您可以覆盖 Fragment 的 setUserVisibleHint() 方法并加载那里的复选框。

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(isVisibleToUser)setCheckBox();
}

这只是一种解决方法,我认为这不是解决它的最佳方法,但我还没有找到更好的方法。这可能会导致 Null 指针异常。

【讨论】:

  • 我们可以通过空检查器避免 NPE。在访问任何对象之前始终保留 null 检查器
【解决方案3】:

我创建了这个Kotlin extension property 来解决这个问题(显然只有在你编写 Kotlin 代码而不是 Java 时才有用)

/**
 * Set the state of a checkbox skipping animations.
 */

var CheckBox.checked: Boolean
    get() = isChecked
    set(value) {
        if(isChecked != value) {
            isChecked = value
            jumpDrawablesToCurrentState()
        }
    }

现在在我的代码中我写 checkbox_view.checked = true 而不是 checkbox_view.isChecked = true

【讨论】:

    猜你喜欢
    • 2013-07-04
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-07
    • 2013-08-08
    • 1970-01-01
    相关资源
    最近更新 更多