這裏我顯示一個AlertDialog
按下菜單鍵並顯示彈出式菜單dialogs
,但是當我試圖以dissmiss
對話框時,再次按菜單鍵第二次似乎menu key
沒有觸發。第二次按下菜單鍵時無法關閉AlertDialog
這裏是我這個
@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);
}
爲什麼在Menu按鈕上顯示對話框?不直觀的行爲。 – dannyroa 2013-03-27 05:27:57
我知道,但那是我的要求。 – 2013-03-27 05:28:49