警报对话框可以在Android中的运行时更改标题

时间:2012-12-24 05:33:29

标签: android android-alertdialog android-dialog

在Android中,我可以在运行时更改对话框的标题吗?

AlertDialog.Builder builder;
builder = new AlertDialog.Builder(Manage_Holidays.this);
builder.setMessage("Are you sure you want to insert new holiday")
.setCancelable(false)
.setTitle("Confirmation")
.setNegativeButton("No",new DialogInterface.OnClickListener() {
    public void onClick(    DialogInterface dialog,int id) {
                   // Title need to changed ass progress                            }})
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
    public void onClick(    DialogInterface dialog,int id) {
    builder.setMessage("Processing...");
    builder.setTitle("dsjc");
    }}).setCancelable(false);
 AlertDialog alert = builder.create();
 alert.show();

在此,我想在点击按钮时显示一个字符串,并为按钮显示另一个字符串.. =

2 个答案:

答案 0 :(得分:1)

DialogInterface.OnClickListener dialogClickListener = new  DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
    switch (which){
    case DialogInterface.BUTTON_POSITIVE:
        //Yes button clicked
        break;

    case DialogInterface.BUTTON_NEGATIVE:
        //No button clicked
        break;
    }
}
};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();

如果您有其他是/否框,您也可以重复使用DialogInterface.OnClickListener

答案 1 :(得分:1)

您无法重复使用相同的提醒对话框

 final AlertDialog.Builder builder,anotherbuilder;
     builder = new AlertDialog.Builder(MainView.this);
     anotherbuilder =new AlertDialog.Builder(this);
     builder.setMessage("Are you sure you want to insert new holiday")
     .setCancelable(false)
     .setTitle("Confirmation")
     .setNegativeButton("No",new DialogInterface.OnClickListener() {
         public void onClick(    DialogInterface dialog,int id) {
                        // Title need to changed ass progress                   
             }})
     .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
         public void onClick(    DialogInterface dialog,int id) {
         anotherbuilder.setMessage("Processing...");
         anotherbuilder.setTitle("dsjc");
         AlertDialog alert = anotherbuilder.create();
          alert.show();
         }}).setCancelable(false);


      AlertDialog alert = builder.create();
      alert.show();