且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何改变正负按钮的颜色自定义警报对话框中的Andr​​oid

更新时间:2022-10-18 07:52:42

你可以做这样的 -

 公共无效createDialog(){
    AlertDialog.Builder建设者=新AlertDialog.Builder(本);
    builder.setMessage(你想从应用程序退出);
    builder.setCancelable(假);
    builder.setNegativeButton(否,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
            dialog.cancel();
        }
    });    builder.setPositiveButton(是,新DialogInterface.OnClickListener(){
        公共无效的onClick(DialogInterface对话,诠释它){
            Toast.makeText(MainActivity.this,从应用程序退出
                    Toast.LENGTH_LONG).show();        }
    });    AlertDialog警报= builder.create();
    alert.show();
    按钮n按钮= alert.getButton(DialogInterface.BUTTON_NEGATIVE);
    nbutton.setBackgroundColor(Color.MAGENTA);
    按钮pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
    pbutton.setBackgroundColor(Color.YELLOW);
}

What i am doing: I am creating a custom alert dialog

What i am trying to do: along with below code, How to change the color of action buttons in dialog(positive and negative)

@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();

you can do it like this-

public void createDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    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(MainActivity.this, "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);
}