android - 为什么 alertDialog 显示在主屏幕上?
android - why alertDialog showing on homescreen?
我在应用程序的 onCreate 期间有一个 alertDialog 提示 Class
(我添加了<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
)
并且我已将 setCancelable 设置为 false,它确实成功阻止了用户按下后退按钮。
这是我目前的成就,也是我想要的。
但是,当我按下主页按钮时,应用程序确实 "disappear" 但不知何故对话框仍然显示在主屏幕上,这是我不想要的。
我期望的是后退按钮应该什么都不做(这是我已经实现的)。
并且当用户单击主页按钮时,整个应用程序包括对话框 应该消失。但是不知何故,当我点击主页按钮时,对话框仍然出现在主屏幕上...
试试这个,
myDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog1, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
这样做就可以了:-
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialogs, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
// Do your stuff here...
return true;
}
return false;
}
});
这是因为您在创建 AlertDialog, you probably pass an ApplicationContext instead of just Context 时。
通过遵循 android 框架指南,您不应在应用程序 class 中进行任何 UI 更改。在你的 Activity of Fragment.
中进行
所以在你的 Activity class:
中这样做
new AlertDialog.Builder(this)
或者在您的片段中:
new AlertDialog.Builder(getContext)
如果您的应用程序进入后台状态,它将消失。
我在应用程序的 onCreate 期间有一个 alertDialog 提示 Class
(我添加了<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
)
并且我已将 setCancelable 设置为 false,它确实成功阻止了用户按下后退按钮。
这是我目前的成就,也是我想要的。
但是,当我按下主页按钮时,应用程序确实 "disappear" 但不知何故对话框仍然显示在主屏幕上,这是我不想要的。
我期望的是后退按钮应该什么都不做(这是我已经实现的)。
并且当用户单击主页按钮时,整个应用程序包括对话框 应该消失。但是不知何故,当我点击主页按钮时,对话框仍然出现在主屏幕上...
试试这个,
myDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog1, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
这样做就可以了:-
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialogs, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
// Do your stuff here...
return true;
}
return false;
}
});
这是因为您在创建 AlertDialog, you probably pass an ApplicationContext instead of just Context 时。
通过遵循 android 框架指南,您不应在应用程序 class 中进行任何 UI 更改。在你的 Activity of Fragment.
中进行所以在你的 Activity class:
中这样做new AlertDialog.Builder(this)
或者在您的片段中:
new AlertDialog.Builder(getContext)
如果您的应用程序进入后台状态,它将消失。