1
我有我的操作欄中的菜單項列表。每個項目點擊應該觸發不同的方法。但onOptionsItemSelected從不被調用。onOptionsItemSelected不叫
這是動作條是如何在MainActivity定義:
public class MainActivity extends AppCompatActivity {
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings_1) {
//do something
return true;
} else if (id == R.id.action_settings_2) {
//do something
return true;
} else if (id == R.id.action_settings_1) {
//do something
return true;
}
return super.onOptionsItemSelected(item);
}
...
}
這是動作條菜單佈局menu_main:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.MainActivity">
<item
android:id="@+id/action_settings_1"
android:orderInCategory="1"
android:title="Item 1"
app:showAsAction="never" />
<item
android:id="@+id/action_settings_2"
android:orderInCategory="2"
android:title="Item 2"
app:showAsAction="never" />
<item
android:id="@+id/action_settings_3"
android:orderInCategory="3"
android:title="Item 3"
app:showAsAction="never" />
</menu>
我如何可以設置動作條,使onOptionsItemSelected被稱爲當操作欄項目被點擊?