【链接】 https://www.jianshu.com/p/e9130d31ee1f
【链接】 https://www.jianshu.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;
}