无法找到复选框的样式

时间:2018-06-08 14:20:35

标签: java android

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

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

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>

然后我在我的片段中调用了这个,因为我创建了我的复选框,但是R.id.checkboxStyle即将出现,但不确定在哪里放置此ID或者我需要以其他方式引用它< / p>

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>

1 个答案:

答案 0 :(得分:3)

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

它不是id,而是style,您需要像这样访问它

 R.style.checkboxStyle;
相关问题