gpt4 book ai didi

java - 找不到复选框的样式

转载 作者:行者123 更新时间:2023-11-29 04:18:26 25 4
gpt4 key购买 nike

我想以编程方式将我动态创建的复选框设置为单选按钮,因为我喜欢这种外观。

为此,我创建了一个 checkboxStyle.xml 并将其放在 res 文件夹的值部分。

checkboxStyle.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="checkboxStyle">
<item name="android:checkboxStyle">@android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton</item>
</style>
</resources>

然后我在我的 fragment 中调用它,因为我创建了我的复选框,但是 R.id.checkboxStyle 正在出现,因为没有找到,但我不确定把这个 id 放在哪里或者我是否需要换种方式引用它

CheckBox checkBox;

ArrayList<String> list = new ArrayList<>();
list.add("test");
list.add("this");
list.add("thing");

final ArrayList<CheckBox> checkBoxArray = new ArrayList<>();
for(int i = 0; i < list.size(); i++){
checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);
checkBox.setId(i);
checkBox.setText(list.get(i));
checkBox.setTag(list.get(i));

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Log.v("CheckBox - checked", buttonView.getTag().toString());
}else{
Log.v("CheckBox - unchecked", buttonView.getTag().toString());
}
}
});

checkBoxArray.add(checkBox);
checkboxContainer.addView(checkBox);
}

testing_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/checks"
android:orientation="vertical">

</LinearLayout>

<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

最佳答案

checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);

它不是一个id,它是一个style,你需要像这样访问它

 R.style.checkboxStyle;

关于java - 找不到复选框的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50762704/

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