如何更改android中自定义警报对话框中的正负按钮的颜色

Dev*_*ath 15 android android-alertdialog

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

我想做什么:以及下面的代码,如何更改对话框中的操作按钮的颜色(正面和负面)

@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();
Run Code Online (Sandbox Code Playgroud)

Cha*_*rma 34

你可以这样做 -

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 有没有办法通过样式或主题来做到这一点?对许多或每个对话框执行此操作似乎不是一个好主意 (2认同)

小智 9

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),R.style.DialogeTheme);
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


小智 6

你可以覆盖onStart方法来处理所有按钮(正,负和中性).

@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));
}
Run Code Online (Sandbox Code Playgroud)


小智 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);
Run Code Online (Sandbox Code Playgroud)