我在menu_main.xml中有这些菜单项
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
android:orderInCategory="1" />
android:orderInCategory="2" />
android:orderInCategory="3" />
android:orderInCategory="4" />
android:orderInCategory="5" />
我在我的onOptionsItemSelected方法中有这个:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_restart) {
Toast.makeText(MainActivity.this, "Restart...", Toast.LENGTH_LONG).show();
}
if (id == R.id.action_clear) {
Toast.makeText(MainActivity.this, "Clear...", Toast.LENGTH_LONG).show();
}
if (id == R.id.action_update) {
Toast.makeText(MainActivity.this, "Update...", Toast.LENGTH_LONG).show();
}
if (id == R.id.action_about) {
Toast.makeText(MainActivity.this, "About...", Toast.LENGTH_LONG).show();
}
if(id == R.id.action_try_restart) {
// how to click / trigger the "action_restart" from here?
}
return super.onOptionsItemSelected(item);
}
我试过:
MenuItem actionRestart = (MenuItem) findViewById(R.id.action_restart);
actionRestart; //
但是actionRestart引用不提供任何类似的点击,触发器等.
我也想注意到,我是Android开发的新手,我来自PHP / JavaScript背景,所以这个级别的Java OOP对我而言都是新的.