您最初的问题不太清楚您想做什么。因此,如果您想在用户在 - 中键入内容后按下后退按钮时显示一个对话框,EditText那么您需要Activity 类中的方法@Override。onBackPressed
@Override
public void onBackPressed() {
// Here you want to show the user a dialog box
new AlertDialog.Builder(context)
.setTitle("Exiting the App")
.setMessage("Are you sure?")
.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// The user wants to leave - so dismiss the dialog and exit
finish();
dialog.dismiss();
}
}).setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// The user is not sure, so you can exit or just stay
dialog.dismiss();
}
}).show();
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处查看已接受的答案。
但是,如果您只想处理对话框本身的后退按钮按下操作,那么这个问题已经有了答案 - 基本上您可以OnKeyListener在对话框上设置一个像这样的:
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
/* The user pressed back button - do whatever here.
Normally you dismiss the dialog like dialog.dismiss(); */
}
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
看看这里接受的答案。
| 归档时间: |
|
| 查看次数: |
13891 次 |
| 最近记录: |