在这里,我显示了一个AlertDialog
通过按菜单键并显示弹出窗口dialogs
,但是当我dissmiss
第二次再次按菜单键尝试进入对话框时,它似乎 menu key
没有触发。
这是我的代码
@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" })
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == 0 ) {
if(!open){
showDialogItem();
open=true;
}
else if(open){
alertDialog.dismiss(
open =false;
}return true;
}
return super.onKeyDown(keyCode, event);
}
public void showDialogItem(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
String[] items = getResources().getStringArray(R.array.select_dialog_items);
new AlertDialog.Builder(MainActivity.this)
.setMessage("You selected: " + which + " , " + items[which])
.show();
}
});
alertDialog = alertDialogBuilder.create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp = alertDialog.getWindow().getAttributes();
lp.gravity = Gravity.TOP | Gravity.LEFT;
lp.x=-300;
lp.gravity=Gravity.BOTTOM;
alertDialog.getWindow().setAttributes(lp);
alertDialog.show();
alertDialog.getWindow().setLayout(130, 220);
}