我遵循了本教程,该教程在此行中返回 True 时显示菜单选项:myMenuInflater.inflate(R.menu.menu, menu);
当值返回 false 时,如何添加另一个菜单选项?我想在同一个菜单按钮中显示 2 个不同的菜单选项。我的代码:
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidOptionMenu extends Activity {
/** Called when the activity is first created. */
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater myMenuInflater = getMenuInflater();
myMenuInflater.inflate(R.menu.menu, menu);
return true;
}
@Override public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId()) {
case(R.id.menu_OK):
Toast.makeText(this, "OK", Toast.LENGTH_LONG).show();
break;
case(R.id.menu_help):
Toast.makeText(this, "Help", Toast.LENGTH_LONG).show();
break;
case(R.id.menu_Cancel):
Toast.makeText(this, "Cancel", Toast.LENGTH_LONG).show();
break;
}
return true;
}
}