Android 对话框在任何物理按键时关闭
Android dialog closes on any physical keypress
我有这个对话框,只有一个文本视图,没有按钮。它显示一些信息,我需要更改左右按键的信息。不幸的是,对话框关闭任何键。
MainActivity中这段代码调用对话框(省略多余代码)
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
if (event.getAction() == KeyEvent.ACTION_UP) {
InfoDialog infoDialog = new InfoDialog();
infoDialog.showDialog(this,currentDateAndTime,chn);
return true;
}
}
return super.dispatchKeyEvent(event);
}
这是对话代码
public class InfoDialog {
private int counter = 0;
private Dialog dialog;
public void showDialog(final Activity activity, final String DateTime, final ChannelList.Channel chn){
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.info_dialog);
counter = 0;
setDialogText(chn,DateTime,counter);
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
if (counter>0) counter--;
setDialogText(chn,DateTime,counter);
return true;
}
}
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
if (counter<chn.infoText.size()-1) counter++;
setDialogText(chn,DateTime,counter);
return true;
}
}
dialog.dismiss();
return false;
}
});
dialog.show();
}
}
当我按下任何按钮时,对话框关闭,即使 onKey 已被调用。
我错过了什么?如何仅处理我的对话框的按键? (其他对话框可能使用相同的按键进行不同的操作)
好吧,在 InfoDialog
中您正在设置 DialogInterface.OnKeyListener
并且在 onKey
方法的底部您正在调用 dialog.dismiss()
,尝试删除此行...(并且 return true
对于任何情况)
我有这个对话框,只有一个文本视图,没有按钮。它显示一些信息,我需要更改左右按键的信息。不幸的是,对话框关闭任何键。 MainActivity中这段代码调用对话框(省略多余代码)
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
if (event.getAction() == KeyEvent.ACTION_UP) {
InfoDialog infoDialog = new InfoDialog();
infoDialog.showDialog(this,currentDateAndTime,chn);
return true;
}
}
return super.dispatchKeyEvent(event);
}
这是对话代码
public class InfoDialog {
private int counter = 0;
private Dialog dialog;
public void showDialog(final Activity activity, final String DateTime, final ChannelList.Channel chn){
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.info_dialog);
counter = 0;
setDialogText(chn,DateTime,counter);
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
if (counter>0) counter--;
setDialogText(chn,DateTime,counter);
return true;
}
}
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
if (counter<chn.infoText.size()-1) counter++;
setDialogText(chn,DateTime,counter);
return true;
}
}
dialog.dismiss();
return false;
}
});
dialog.show();
}
}
当我按下任何按钮时,对话框关闭,即使 onKey 已被调用。 我错过了什么?如何仅处理我的对话框的按键? (其他对话框可能使用相同的按键进行不同的操作)
好吧,在 InfoDialog
中您正在设置 DialogInterface.OnKeyListener
并且在 onKey
方法的底部您正在调用 dialog.dismiss()
,尝试删除此行...(并且 return true
对于任何情况)