我有两个AlertDialogs,并从第一AlertDialog我来第二次。我的目标是修改标题从第一个AlertDialog使用第二个。Android的 - 最后修改标题AlertDialog
当我尝试使用该方法的setTitle(),它告诉我,alertDialog2必须是最终的,但如果我设置对象alertDialog2到最终,我不能修改其设置alertDialog2的称号。
AlertDialog alertDialog2 = new AlertDialog.Builder(MainActivity.this).create();
place="";
alertDialog2.setTitle(place); //this is the original place
alertDialog2.setMessage(text);
alertDialog2.setButton(Dialog.BUTTON_NEUTRAL, "Set Place", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final CharSequence[] items = {" Place1 "," Place2 "," Place3 "," Place4 "};
// Creating and Building the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select Place");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item)
{
case 0:
// Your code when first option seletced
place=String.valueOf(items[0]);
break;
case 1:
// Your code when 2nd option seletced
place=String.valueOf(items[1]);
break;
case 2:
// Your code when 3rd option seletced
place=String.valueOf(items[2]);
break;
case 3:
// Your code when 4th option seletced
place=String.valueOf(items[3]);
break;
}
}
});
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
alertDialog2.setTitle(place); // Here I can not set the title
alertDialog2.show();
}
});
builder.create();
builder.show();
}
});
alertDialog2.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Canceled", Toast.LENGTH_SHORT).show();
}
});
alertDialog2.show();
这不是一个回答你的问题,但像这样的对话框(警告和progressDialogs过),你需要妥善处理关闭它们,我会建议将它们设置为null,一旦他们已经关闭。对话框容易发生内存泄漏。 使用: 如果(!DialogInterface!= null){ DialogInterface.close(); DialogInterface = null; } –
这将是最好只使用'DialogFragment'您的所有对话。 – ashishduh
感谢您的建议。 – david