【问题标题】:How can I change the text color and the tint color of radiobuttons in a Alert?如何更改警报中单选按钮的文本颜色和色调颜色?
【发布时间】:2020-10-17 12:51:26
【问题描述】:

现在我有一个按钮,点击它会弹出一个带有 3 个单选按钮的 AlertDialog。

我想更改文本颜色和色调颜色以匹配我在应用程序其余部分的颜色。除了对方法的调用之外,这是我用于警报的所有代码:

public void CreateAlertDialogWithRadioButtonGroup()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(Settings.this, R.style.myDialogTheme);

        if(sharedPreferences.getString("units", "").equals("metric"))
            chosenUnit = 0;
        else if(sharedPreferences.getString("units", "").equals("imperial"))
            chosenUnit = 1;
        else
            chosenUnit = 2;

        builder.setTitle("Select the Unit for the Temperature");

        builder.setSingleChoiceItems(values, chosenUnit, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int item)
            {
                switch(item)
                {
                    case 0://Celsius
                        unitsEditor.putString("units", "metric");
                        unitsEditor.commit();
                        break;
                    case 1://Fahrenheit
                        unitsEditor.putString("units", "imperial");
                        unitsEditor.commit();
                        break;
                    case 2://Kelvin
                        unitsEditor.putString("units", "");
                        unitsEditor.commit();
                        break;
                }
                alertDialog1.dismiss();
            }
        });
        alertDialog1 = builder.create();
        alertDialog1.show();
    }

我试图改变它们的样式,但都不起作用,我似乎只能改变警报的文本颜色而不是单选按钮。任何想法?我没有警报的布局,所以我不能为无线电组做 findById。我在 Java 而不是 Kotlin 中这样做。

【问题讨论】:

    标签: java android-studio radio-button android-alertdialog


    【解决方案1】:

    将 app:buttonTint 选项添加到 RadioButton XML,如下所示:

    <RadioButton
            android:id="@+id/radio_btn_metric"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:buttonTint="@color/colorRed" />
    

    另外,转到 color.xml 并添加您想要成为 RadioButton 的颜色

    【讨论】:

    • 这行不通,因为我没有为警报使用单独的布局
    • 您可以在 myDialogTheme 中更改 style.xml 中的主色和强调色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多