在 Android 中更改 AlertDialog 内的 CheckBox 样式

Change CheckBox Style inside AlertDialog in Android

我在更改警报对话框的复选框主题时遇到问题。我无法将选中的复选框颜色更改为我的 AccentColor。

这是我的风格代码

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">      
<item name="android:checkboxStyle">@style/CustomCheckBox</item>
</style>
<style name="CustomCheckBox"     parent="android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/activated_checkbox</item>
</style>

选择器:activated_checkbox

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- checked -->
<item android:state_checked="true" android:drawable="@color/AccentColor">
</item>
<item android:state_checked="false" android:drawable="@color/White">
</item>
</selector>

和显示 AlertDialog 的方法。

private void showDialog() {

    final CharSequence[] items = getResources().getStringArray(R.array.tipoDescargas);
    final boolean[] states = {false, false, false};
    AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.AlertDialogCustom);
    builder.setTitle(getResources().getString(R.string.preguntaDescargas));
    builder.setMultiChoiceItems(items, states, new DialogInterface.OnMultiChoiceClickListener() {
        public void onClick(DialogInterface dialogInterface, int item, boolean state) {
        }
    });
    builder.setPositiveButton("Descargar", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            SparseBooleanArray CheCked = ((AlertDialog) dialog).getListView().getCheckedItemPositions();
            if (CheCked.get(0) && CheCked.get(1) && CheCked.get(2))
                Toast.makeText(getApplication(), "TODOS", Toast.LENGTH_SHORT).show();
            if (CheCked.get(0)) {
                Toast.makeText(getApplication(), "Item 1", Toast.LENGTH_SHORT).show();
            }
            if (CheCked.get(1)) {
                Toast.makeText(getApplication(), "Item 2", Toast.LENGTH_SHORT).show();
            }
            if (CheCked.get(2)) {
                Toast.makeText(getApplication(), "Item 3", Toast.LENGTH_SHORT).show();
            }
        }
    });
    builder.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });builder.create().show();
}

谁能帮帮我?谢谢

更新 1

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:checkedTextViewStyle">@style/CustomCheckBox</item>
</style>

<style name="CustomCheckBox" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:checkMark">@drawable/activated_checkbox</item>
</style>

解决方案

我找到了正确显示颜色的解决方案。

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:listChoiceIndicatorMultiple">@drawable/activated_checkbox</item>
</style>

和选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkedcheckbox24"     android:state_checked="true"></item>
<item android:drawable="@drawable/uncheckedcheckbox24"     android:state_checked="false"></item>
</selector>

您可以使用自定义选择器来自定义您的复选框。

<CheckBox
    android:text="Custom CheckBox"
    android:button="@drawable/checkbox_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

你的选择器class,把它放在drawable文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/checkedimage" />
    <item android:state_checked="false" android:drawable="@drawable/uncheckedimage" />
</selector>

AlertDialog 用于显示每个选项的默认多选布局实际上是 CheckedTextView。由于此 class 不会以任何方式扩展 CheckBox,因此您主题中的 android:checkboxStyle 引用将无效。

API level 17 增加了属性android:checkedTextViewStyle,可以用来专门给CheckedTextView设置样式。如果您的最低 sdk 版本设置为此或任何更高版本,那么您可以使用这种方法。

另一种在旧平台上也可用的替代方法是 android:listChoiceIndicatorMultiple(对于单选项目也有一个等效项,或者说得更好:视觉上看起来像单选按钮的项目,但同样是 CheckedTextView 在现实中)。使用它来提供您自己的可绘制对象,该可绘制对象将显示为复选标记(或单选按钮圆圈)。

使用此代码,图标可以正确显示

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:listChoiceIndicatorMultiple">@drawable/activated_checkbox</item>
</style>

和选择器代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkedcheckbox24"     android:state_checked="true"></item>
<item android:drawable="@drawable/uncheckedcheckbox24"     android:state_checked="false"></item>
</selector>