我有一个带有输入字段和两个按钮的警告对话框(还原,保存)。 当我点击手机上的“后退”按钮时,我想要另一个确认对话框弹出询问:“你确定要完成吗?”。所以这一切看起来像这样:
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();
}
一切正常,但只有在我按任意按钮两次后才会关闭第二个确认对话框(否,是)。 我似乎无法找到原因。 谢谢。
答案 0 :(得分:3)
OnKey方法被调用两次:第一次按下键,第二次按键,所以你必须过滤:
所以改为如下代码
newItemalertDialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
if (event.getAction() != KeyEvent.ACTION_DOWN)
return true;
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(MemberShipActivity.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.cancel();
newItemalertDialog.dismiss();
}
}).setNegativeButton("No", null).show();
Log.e("Key","back");
}
return false;
}
});
答案 1 :(得分:0)
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
试试这段代码。 当你按回 OnBackPressed 方法时会调用。所以你可以在那里添加提示。