如何改变正负按钮的颜色自定义警报对话框中的Android [英] How to change the colour of Positive and negative button in Custom Alert dialog in android
本文介绍了如何改变正负按钮的颜色自定义警报对话框中的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在做什么:我创建一个自定义警告对话框
我试图做的:低于code一起,如何更改操作按钮的颜色对话框(正面和负面的)
@覆盖
公共对话框onCreateDialog(捆绑savedInstanceState){
AlertDialog.Builder建设者=新AlertDialog.Builder(getActivity());
//获取吹气布局
LayoutInflater吹气= getActivity()getLayoutInflater()。 //充气并为对话设置布局
作为父视图//传递空值,因为它在该对话框的布局会
builder.setView(inflater.inflate(R.layout.dialog_signin,NULL))
//添加动作按钮
.setPositiveButton(R.string.signin,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释的id){
// SIGN在用户...
}
})
.setNegativeButton(R.string.cancel,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
LoginDialogFragment.this.getDialog()取消()。
}
});
返回builder.create();
解决方案
你可以做这样的 -
公共无效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);
}
这篇关于如何改变正负按钮的颜色自定义警报对话框中的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文