.

//按下
public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_VOLUME_UP){
                Toast.makeText(mainActivity.this, "按下了上音量鍵", Toast.LENGTH_LONG).show();
                return true;
        }else if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN){
                Toast.makeText(mainActivity.this, "按下了下音量鍵", Toast.LENGTH_LONG).show();
                return true;
        }else if(keyCode==KeyEvent.KEYCODE_BACK){
            Toast.makeText(mainActivity.this, "按下了返回鍵", Toast.LENGTH_LONG).show();
            return true;
        }else if(keyCode==KeyEvent.KEYCODE_HOME){
            Toast.makeText(mainActivity.this, "按下了HOME鍵", Toast.LENGTH_LONG).show();
            return true;
        }else if(keyCode==KeyEvent.KEYCODE_MENU){
            Toast.makeText(mainActivity.this, "按下了Menu鍵", Toast.LENGTH_LONG).show();
            return super.onKeyDown(keyCode, event);
        }else if(keyCode==KeyEvent.KEYCODE_CAMERA){
            Toast.makeText(mainActivity.this, "按下了相機", Toast.LENGTH_LONG).show();
            return true;
        }else if(keyCode==KeyEvent.KEYCODE_DPAD_DOWN){
            Toast.makeText(mainActivity.this, "按下了下", Toast.LENGTH_LONG).show();
            return true;
        }else if(keyCode==KeyEvent.KEYCODE_SEARCH){
            Toast.makeText(mainActivity.this, "按下了搜尋", Toast.LENGTH_LONG).show();
            return true;
} return super.onKeyDown(keyCode, event); }
 //按下的鍵起來
public boolean onKeyUp(int keyCode,KeyEvent keyevent){
        if(keyCode==keyevent.KEYCODE_MENU){
            Toast.makeText(mainActivity.this, "Menu Up", Toast.LENGTH_LONG).show();
            return true;
        }
        return super.onKeyUp(keyCode, keyevent);
    }
//長按
public boolean onKeyLongPress(int keyCode,KeyEvent keyevent){
       //...to do
return super.onKeyLongPress(keyCode, keyevent);
}

以上都可以跑但是會影響到Menu的onCreateOptionsMenu()跑不出來
改進>>把return  true 都改成  
//return super.onKeyDown(keyCode, event);
或是把return  true都去掉
OK

文章標籤
創作者介紹

Mazs's Notes

cookiesp 發表在 痞客邦 PIXNET 留言(0) 人氣()