0

我想在如下图所示的对话框中更改按钮按下的颜色。

在此处输入图像描述

使用简单的警报对话框时,我设法做到了这一点。

AlertDialog.Builder builder = new AlertDialog.Builder(this);

// Set dialog title
builder.setTitle(Html.fromHtml("<font color='#8b0000'>"
                + getResources().getString(R.string.delete_this_list)
                + "?" + "</font>"));

builder.setMessage(getResources().getString(R.string.delete)
                + " '"
                + lists[listNo]
                + "'?\n"
                + getResources().getString(
                        R.string.delete_this_list_description))
        .setCancelable(false)
        .setPositiveButton(getResources().getString(R.string.delete),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.dismiss();

                        //Some background stuff
                        //.......//
                    }
                })
        .setNegativeButton(
                getResources().getString(android.R.string.cancel),
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                });
alertDialog = builder.create();
alertDialog.show();

alertDialog.getButton(DialogInterface.BUTTON_POSITIVE)
                .setBackground(getResources().getDrawable(R.drawable.btn_bar_holo_red));
alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE)
            .setBackground(getResources().getDrawable(R.drawable.btn_bar_holo_red));

但是,这种方法在使用 DialogFragments 时不起作用。使用 DialogFragments 时,没有 getButton 方法。

有任何想法吗??

4

1 回答 1

2

代替:

alertDialog.getButton(DialogInterface.BUTTON_POSITIVE) 。setBackground (getResources().getDrawable(R.drawable.btn_bar_holo_red));

尝试:

alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        Button positiveButton = ((AlertDialog) dialog)
              .getButton(AlertDialog.BUTTON_POSITIVE);
        positiveButton.setBackgroundResource(R.drawable.btn_bar_holo_red_full);

        Button negativeButton = ((AlertDialog) dialog)
              .getButton(AlertDialog.BUTTON_NEGATIVE);
        negativeButton.setBackgroundResource(R.drawable.btn_bar_holo_red_full);
    }
});

btn_bar_holo_red_full 将在它自己的 xml 文件中定义如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true" />
    <item android:drawable="@drawable/button_focused" android:state_focused="true" />
    <item android:drawable="@drawable/button_default" />
</selector>

然后定义 button_pressed、button_focused 和 button_default 如下(你需要三个,一个用于按下、聚焦和默认):

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" androuid:shape="rectangle">
    <solid android:color="@color/btn_background_pressed" />
</shape>

剩下要做的就是将这些颜色值添加到您的 color.xml 文件中。

于 2014-11-08T18:06:23.697 回答