我有兩個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