gpt4 book ai didi

java - 复选框值为 true 但勾号未显示 - Android/Java

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:19 25 4
gpt4 key购买 nike

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

第一个 fragment 页面:

enter image description here

第二个 fragment 页面:

enter image description here

设置复选框的代码及其if条件

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);
}
}

最佳答案

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

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

尝试在 Root View (或复选框的父 View )上调用 jumpDrawablesToCurrentState。如果像 OP 那样专门调用 checkBox.setChecked(true);,请在之后调用它。在这个问题中,在所有复选框都被添加到父 View 之后调用它是很重要的。这基本上会绘制当前状态下的所有可绘制对象,跳过动画过程。

例子:

@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();
}
}

关于java - 复选框值为 true 但勾号未显示 - Android/Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44254269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com