更改AlertDialog中按钮的颜色

56

如何在 Android 的 AlertDialog 中更改按钮的颜色?


18个回答

2

我认为有些开发者希望扩展AlertDialog类并定义类定义中的按钮颜色。为此,您可以使用以下代码:

class MyDialog extends AlertDialog {
    public MyDialog(final Context context) {
        super(context); 
        setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button negativeButton = getButton(DialogInterface.BUTTON_NEGATIVE);  
                Button positiveButton = getButton(DialogInterface.BUTTON_POSITIVE);

                negativeButton.setBackgroundColor(Color.GREEN);
                positiveButton.setBackgroundColor(Color.RED);
            }
        });
    }
}

2

以下是对我有效的完美解决方案:

AlertDialog.Builder alertDialogBuilder
                        = new AlertDialog.Builder(DashboardActivity.this);
alertDialogBuilder.setTitle("");
alertDialogBuilder.setMessage("Are you sure you want to Logout?");

alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        preferenceManager.logout();
        Intent intent = new Intent(DashboardActivity.this,
                                LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }
});

alertDialogBuilder.setNegativeButton("Cancel", null);

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

Button btnOk = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
Button btnCancel = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);

if (btnOk != null && btnCancel != null) {       
    btnOk.setTextColor(getResources().getColor(R.color.colorGreenButton));
    btnCancel.setTextColor(getResources().getColor(R.color.colorGreenButton));
} else {
    Log.i(TAG, "LogOut: Buttons of Dialog are null");
}

1

更改AlertDailog的按钮颜色

代码:

// Initialize AlertDialog & AlertDialog Builder
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setTitle(R.String.AlertDialogTitle);
...........
......... 
//Build your AlertDialog 
AlertDialog Demo_alertDialog= builder.create();
Demo_alertDialog.show();

//For Positive Button:
Button b_pos; 
b_pos=Demo_alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
if(b_pos!=null){
   b_pos.setTextColor(getResources().getColor(R.color.YourColor));
   }    


//For Neutral Button:
Button b_neu;
b_neu=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
if(b_neu!=null){
   b_neu.setTextColor(getResources().getColor(R.color.YourColor));
   }

//For Negative Button:
Button b_neg;
b_neg=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b_neg!=null){
   b_neg.setTextColor(getResources().getColor(R.color.YourColor));
   }

1
如果您正在使用DialogFragment(android.app.DialogFragment),那么您可以重写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));
}

所有上述解决方案都适用于在同一活动或片段中创建的AlertDialog或Dialog,但不适用于单独创建的DialogFragment。

0

不,您无法更改警告框的默认按钮的颜色、图像或背景。如果要进行自定义,则需要创建自己的自定义对话框,如下所示。

public class TryAgainAlert extends Dialog implements OnClickListener
{
    @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
  if (keyCode == KeyEvent.KEYCODE_BACK)
  {   

   Intent i = new Intent(getApplicationContext(), MainMenu.class);
   finish();
   startActivity(i);

   return true;
  }
  return super.onKeyDown(keyCode, event);
 }


    TextView scores;
    Button tryagain,mainmenu,submit;


     public TryAgainAlert(Context context) {
        super(context);

        setContentView(R.layout.tryagainalert);

        scores=(TextView)findViewById(R.id.text);



        tryagain= (Button) findViewById(R.id.trya);
        mainmenu= (Button) findViewById(R.id.submitscore);
        submit= (Button) findViewById(R.id.mainmenu);

    }


    @Override
    public void onClick(View v) {
        if(v == tryagain)
        {

        else if (v==mainmenu)
        {


        }
        else if (v == submit)
        {

        }
    }

}

你可以随意处理这个XML文件。 希望能对你有所帮助。 谢谢。


0

只需将字符串设置为可扩展的,并将其传递给setPositiveButton或setNegativeButton,就像这样:

val n=getString(R.string.reject).toSpannable() n.setSpan(ForegroundColorSpan(Color.RED),0,6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) n.setNegativeButton(n)


0

这是如何做到的:

// Initializing a new alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.message);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        doAction();
    }
});
builder.setNegativeButton(R.string.cancel, null);

// Create the alert dialog and change Buttons colour
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface arg0) {
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.red));
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.blue));
        //dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(getResources().getColor(R.color.black));
    }
});
dialog.show();

-1
你是指中性、积极和消极按钮吗?还是指在布局中包含的按钮?
如果是前者,那么是的,你可以。请参考此教程中的自定义按钮部分。你基本上需要一个XML文件,告诉你的按钮每个状态变化要使用哪个Drawable/颜色。然后,你可以将该XML文件设置为按钮的背景。

没有人,我已经完成了那个。但是我必须更改积极、中性和消极按钮的背景... - user644458

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