android - 在 Android 的 AlertDialog 中更改 CheckBox 样式

标签 android checkbox selector android-alertdialog

我在更改警报对话框的复选框主题时遇到问题。我无法将选中的复选框颜色更改为我的 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>

最佳答案

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

<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>

关于android - 在 Android 的 AlertDialog 中更改 CheckBox 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32761557/

相关文章:

android - 如何更改 createCircularReveal() 颜色?

java - MenuInflater 究竟是如何工作的?

android - 有没有一种使用for循环初始化编号为1-10的多个按钮的快速方法? (安卓应用程序)

javascript - 发送通过 AJAX 更改的元素的值

xcode - 从函数 Xcode/Swift 更改 label.text

javascript - 使用 filter() 根据父级过滤 jquery

android - 同一个项目,但可以相互定制和安装?

javascript - 警告所有在 ng-Repeat : AngularJS 中选中的复选框

jsf - 在 JSF/PrimeFaces 中显示复选框列表

jQuery:选择器问题(最接近)