在Activity類的子類中直接復寫下面三個方法:
復制代碼 代碼如下:
//復寫onCreateOptionsMenu()方法,彈出菜單欄
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//復寫back按鈕,如果只是復寫返回按鈕,復寫Activity中的onBackPressed()方法就可以
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Toast.makeText(MainActivity.this, "onBackPressed!!", Toast.LENGTH_SHORT).show();
}
// 復寫搜索按鈕
// 復寫onKeyDown()方法,該方法可以復寫很多按鈕:數字鍵、Menu鍵、返回鍵等等
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_MENU){
Toast.makeText(MainActivity.this, "Search Menu", Toast.LENGTH_SHORT).show();
}
return super.onKeyDown(keyCode, event);
}