我創建了一個帶有兩個菜單項的操作欄。當我嘗試使用按鈕的意圖他們不工作。當點擊菜單項時,網頁將被加載。怎麼做。操作欄菜單項不起作用
的main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
yourapp:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/camera"
android:icon="@drawable/ic_menu_camera"
android:title="Camera"
yourapp:showAsAction="always"/>
<item
android:id= "@+id/emoticons"
android:icon="@drawable/ic_menu_emoticons"
android:title="Emoticon"
yourapp:showAsAction="always"/>
</menu>
MainActivity.java:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean OnOptionsItemSelected(MenuItem item){
switch(item.getItemId()) {
case R.id.camera:
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")));
return true;
case R.id.emoticons:
startActivity(new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
感謝
@developerknownas在按鈕OP的意思下,菜單項。 – user3291365