Android: AlertDialog中RadioButton的自定义样式

Android: Custom style of RadioButton in AlertDialog

我正在像这样在 AlertDialog 中构建 RadioGroup:

val items = listOf("1", "2")

AlertDialog.Builder(activity, R.style.MyStyle)
        .setSingleChoiceItems(items.toTypedArray(), 0) { dialog, index ->
            dialog.dismiss().   
        }
        .show()

我想要的结果是让这个 AlertDialog 看起来像这样:

因此,对于样式,我需要删除实际的 RadioButton 可绘制对象,然后让当前选定的 RadioButton 具有灰色背景。

如何使用 Android 样式实现此设计?

此数据显示在列表中? 还是它的硬编码数据?

如果是listview或recyclerview,很容易实现你想要的。 在你的适配器中你只需这样做

 holder.cardlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check=true; // this is local Boolean  type variable
                previousposition = position;  // previouspostion is local int type var 

                notifyDataSetChanged();
            }
        });
        if (check) {
            if (previousposition == position) {

               //set color of card gray here

            } else {
               //set color of card is white here

            }
        }

请记住它适用于您使用 listview/recyclerview

以下是如何使用 AlertDialog.Builder 完成该样式。

styles.xml中:

<style name="MyStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:listChoiceIndicatorSingle">@null</item>
    <item name="android:background">@drawable/radio_button_background_selector</item>
</style>

radio_button_background_selector.xml:

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