复制代码 代码如下:
//复写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);
}