【鏈接】 http://www.reibang.com/p/e9130d31ee1f
【鏈接】 http://www.reibang.com/p/ae0013a4f71a
【菜單選項點擊事件】
【法1】
//設置導航圖標玄货、添加菜單點擊事件要在setSupportActionBar方法之后
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.mipmap.ic_drawer_home);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(MainActivity.this, "Search !", Toast.LENGTH_SHORT).show(); break;
case R.id.action_notifications:
Toast.makeText(MainActivity.this, "Notificationa !", Toast.LENGTH_SHORT).show(); break;
case R.id.action_settings:
Toast.makeText(MainActivity.this, "Settings !", Toast.LENGTH_SHORT).show(); break;
}
return true;
} });
【法2】
/**
* 菜單項點擊事件
*
* @param item
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "設置", Toast.LENGTH_SHORT).show();
break;
case R.id.action_jing:
Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
break;
case R.id.action_share:
Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
break;
}
return true;
}