gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:36 24 4
gpt4 key购买 nike

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

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