Android: alertdialog closes only after the second click of any button
•
Android
I have an alarm dialog box with input fields and two buttons (restore, save). When I click the back button on my phone, I want to pop up another confirmation dialog box and ask, "are you sure you want to finish?". So everything looks like this:
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();
}
Everything is normal, but the second confirmation dialog box will close only after I tap any button twice (no, yes). I can't seem to find the reason. Thank you
resolvent:
The onkey method is called twice: the first time is to press the key, and the second time is to press the key, so you must filter:
So change the following code
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;
}
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码