重寫onKeyDown()和onKeyDown()方法, 比較keyCode是不是需要處理的鍵.
- 如果是, 處理完成後返回true, 表示已經處理了.
- 如果不是, 傳遞給super處理.
(其他鍵也可以同樣方法處理)
package com.AndroidMenuKey;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
public class AndroidMenuKey extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_MENU){
Toast.makeText(this, "MENU Key Down", Toast.LENGTH_LONG).show();
return true;
}else{
return super.onKeyDown(keyCode, event);
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_MENU){
Toast.makeText(this, "MENU Key Up", Toast.LENGTH_LONG).show();
return true;
}else{
return super.onKeyUp(keyCode, event);
}
}
}
沒有留言:
發佈留言