2013-07-22 68 views
1

當我按菜單顯示我有的方法時,它可能嗎?對話框在設備中按下菜單時變得顯示

我只是想這個對話框顯示和關閉,當我在設備按菜單 dialog

這種方法menuplay

private void menuplay(){ 
     final Dialog dialog = new Dialog(this,R.style.Theme_Dialog_costum01); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.dialog_play); 
     dialog.findViewById(R.id.button_play).setOnTouchListener(
       new OnTouchListener() { 
        public boolean onTouch(View v, MotionEvent event) { 
         dialog.dismiss(); 
         //do method 
         return true; 
        } 
       }); 
     dialog.findViewById(R.id.button_highscore).setOnTouchListener(
       new OnTouchListener() { 
        public boolean onTouch(View v, MotionEvent event) { 
         dialog.dismiss(); 
         highscoreview(); 
         return true; 
        } 
       }); 
     dialog.findViewById(R.id.button_hint).setOnTouchListener(
       new OnTouchListener() { 
        public boolean onTouch(View v, MotionEvent event) { 
         dialog.dismiss(); 
         hint(); 
         return true; 
        } 
       }); 
     dialog.findViewById(R.id.button_about).setOnTouchListener(
       new OnTouchListener() { 
        public boolean onTouch(View v, MotionEvent event) { 
         dialog.dismiss(); 
         aboutview(); 
         return true; 
        } 
       }); 
     dialog.show(); 
    } 

我嘗試這一個,不行

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     menuplay(); 
     return true; 
    } 
+0

你按什麼「菜單」?如果它是一個按鈕,請將'menuplay()'放入按鈕的onClick –

回答

0

將此用於Menu Button

public boolean onKeyDown(int keyCode, KeyEvent event) { 

if (keyCode == KeyEvent.KEYCODE_MENU && event.getAction() == 0) 
{ 
    menuplay(); 
    return true; 

} 
0
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getSupportMenuInflater().inflate(R.menu.menu_main, menu); 
    menuplay(); 
    return super.onCreateOptionsMenu(menu); 
} 
相關問題