ここでは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);
}