我有一个带有输入字段和两个按钮(恢复、保存)的警报对话框。当我点击手机上的“返回”按钮时,我希望弹出另一个确认对话框,询问:“你确定要完成吗?”。所以这一切看起来像这样:
public void showNewItemDialog(final int...position) {
LayoutInflater li = LayoutInflater.from(HostActivity.this);
View promptsView = li.inflate(R.layout.item_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
HostActivity.this);
alertDialogBuilder.setView(promptsView);
userInput = (EditText) promptsView.findViewById(R.id.editTextDialogUserInput);
if(position.length>0){
userInput.setText(listFragment.getmItems().get(position[0]).getTitle());
userInput.setSelection(userInput.length());
userInput.requestFocus();
}
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String title = userInput.getText().toString();
if(listFragment.getItemClickType() == Utility.ItemClick.SHORT){
listFragment.editRowItem(title, position[0]);
}else if(listFragment.getItemClickType() == Utility.ItemClick.LONG){
}else if(listFragment.getItemClickType() == Utility.ItemClick.ADD_BUTTON){
listFragment.addRowItem(title);
}
}
})
.setNegativeButton("Revert",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
newItemalertDialog = alertDialogBuilder.create();
newItemalertDialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(HostActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Add Item")
.setMessage("Are you sure you want to finish?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
newItemalertDialog.dismiss();
}
}).setNegativeButton("No", null).show();
}
return false;
}
});
newItemalertDialog.show();
}
一切正常,但第二个确认对话框只有在我点击任何按钮两次(否,是)后才会关闭。我似乎无法找到造成这种情况的原因。谢谢你。