更改按钮按下Dialog Fragment中的颜色?

时间:2013-12-01 07:00:43

标签: android alertdialog android-dialogfragment dialogfragment

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

enter image description here

使用简单的警报对话框时,我设法完成了这项工作。

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方法。

任何想法??

1 个答案:

答案 0 :(得分: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文件中。