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 を次のように定義します (押された、フォーカスされた、およびデフォルトにそれぞれ 1 つずつ、3 つ必要です)。

<?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 に答える