【问题标题】:Change CheckBox Style inside AlertDialog in Android在 Android 的 AlertDialog 中更改 CheckBox 样式
【发布时间】:2015-12-22 01:39:09
【问题描述】:

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

选择器:激活复选框

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

【问题讨论】:

  • 能否提供一个JSFiddle的工作代码sn-p?

标签: android checkbox selector android-alertdialog


【解决方案1】:

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

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

你的 Selector 类,把它放在 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>

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 我正在尝试使用这个新代码:UPDATE 1,但它不起作用
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多