给Activity添加菜单





android:id="@+id/add_item"
android:title="add"
/>
android:id="@+id/removee_item"
android:title="remove"
/>





//复写
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//给当前Activity,添加菜单
//R.menu.main,添加哪个资源文件
//menu,菜单项,添加到哪个Menu对象
getMenuInflater().inflate(R.menu.main, menu);

return true;//让创建的菜单显示,false,则不显示
}

//复写,菜单响应事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_item:
Toast.makeText(FirstActivity.this, "This is add menu", Toast.LENGTH_LONG).show();
break;

case R.id.removee_item:
Toast.makeText(FirstActivity.this, "This is remove menu", Toast.LENGTH_LONG).show();
default:
break;
}
return true;
}

你可能感兴趣的:(给Activity添加菜单)