如何在Android自定义警告对话框中更改积极和消极按钮的颜色

20

我在做什么:我正在创建一个自定义提示对话框

我想要做什么:除了下面的代码,如何更改对话框中操作按钮(积极和消极)的颜色

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // sign in the user ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();

请查看此链接:(https://dev59.com/B2Yq5IYBdhLWcg3wfgvd) - Prerak Sola
5个回答

42

你可以像这样做 -

public void createDialog(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage("Do you want to exit from app");
    builder.setCancelable(false);
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(context, "You exit from app",
                    Toast.LENGTH_LONG).show();

        }
    });

    AlertDialog alert = builder.create();
    alert.show();
    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setBackgroundColor(Color.MAGENTA);
    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
    pbutton.setBackgroundColor(Color.YELLOW);
}

3
有没有一种通过样式或主题来完成的方法?为每个对话框都这样做似乎不是一个好主意。 - acrespo
1
可以的,为什么不行呢。你也可以通过样式来实现。试试这个链接 -> https://dev59.com/HmQo5IYBdhLWcg3wQdet#24560017 - Chandra Sharma
我不明白那个链接如何解决问题,哪些样式是指正按钮样式,哪些是指负按钮样式。我只看到 android:borderlessButtonStyle - acrespo
你可以创建一个用于生成AlertDialog的实用方法。每次需要对话框时,使用该实用方法即可,无需重复编写代码。 - Mahendra Chhimwal
完美!帮了我很大的忙。 - Aman Alam
alert.show() 之后我们必须使用 getButton 吗?在 alert.show() 之前能否这样做? - Noitidart

16
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.DialogeTheme);

<style name="DialogeTheme" parent="Theme.AppCompat.Dialog.Alert">
    <item name="android:background">@color/all_item_bg</item>
    <item name="colorAccent">@android:color/white</item>
</style>

这将绘制整个对话框。 - Mishka

10

你可以重写 onStart 方法以获取所有按钮(Positive、Negative 和 Neutral)的句柄。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View eventEditDialogView = View.inflate(this.getActivity(), R.layout.event_edit_dialog,
                                            null);

    builder.setTitle(getLocalizedString("edit_event"))
            .setView(eventEditDialogView)
            .setPositiveButton(getLocalizedString("all_events"), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                }
            })
            .setNegativeButton(getLocalizedString("this_event"), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                }
            })
    return builder.create();
}

 @Override
    public void onStart() {
        super.onStart();
    Button positive = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
    positive.setTextColor(Color.BLACK);
    positive.setBackgroundColor(getResources().getColor(R.color.GrayBGColor));
}

6
//***The simpliest solution is:***

dialog.show(); //Only after .show() was called
dialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(neededColor);
dialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(neededColor);

4
您可以使用AlertDialog的setOnShowListener方法来获取按钮并应用不同的颜色和它们自己的监听器,这里有一个Kotlin示例:
val alert : AlertDialog?
val alertView = LayoutInflater.from(context).inflate(R.layout.alert_add_product, null, false)

val builder = AlertDialog.Builder(context)
    builder.setCancelable(false)
    builder.setView(alertView)

    /**Create positive and negative buttons**/
    builder.setPositiveButton(android.R.string.ok, null)
    builder.setNegativeButton(android.R.string.cancel, null)

    alert = builder.create()

    /**Listener called when the AlertDialog is shown**/
    alert.setOnShowListener {

         /**Get the positive button from the AlertDialog**/
         val positiveButton = alert.getButton(DialogInterface.BUTTON_POSITIVE)

         /**Set your color to the positive button**/
         positiveButton.setTextColor(ContextCompat.getColor(context, R.color.bluePrimaryDark))

         /**Set listener to the positive button**/
         positiveButton.setOnClickListener({
              alert.dismiss()
         })

         /**Get the negative button from the AlertDialog**/
         val negativeButton = alert.getButton(DialogInterface.BUTTON_NEGATIVE)

         /**Set your color to the negative button**/
         negativeButton.setTextColor(ContextCompat.getColor(context, R.color.bluePrimaryDark))

         /**Set listener to the negative button**/
         negativeButton.setOnClickListener {
              alert?.dismiss()
         }
    }

    alert.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接