我的menu.xml中有以下代码:
jQuery
案件 <item
android:id="@+id/more"
android:orderInCategory="100"
android:title="@string/goToWebsite"
app:showAsAction="ifRoom" />
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.more:
goToUrl("http://www.example.com");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
未执行:
R.id.more
怎么了?
更新
在r.id.more下工作不能正常工作:
goToUrl("http://www.example.com");
return true;
private void goToUrl (String url) {
Uri uriUrl = Uri.parse(url);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);
}
答案 0 :(得分:0)
在res目录的menu文件夹中创建Menu.xml文件,如:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/common_menu_settings"
android:icon="@drawable/settings"
android:title="@string/settings"
app:showAsAction="ifRoom" />
<item
android:id="@+id/more"
android:title="@string/goToWebsite"
app:showAsAction="ifRoom" />
</menu>
覆盖Activity中的onOptionsItemSelected()。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.common_menu_settings:
onBackPressed();
break;
case R.id.more:
goToUrl("http://www.example.com");
break;
return super.onOptionsItemSelected(item);
}
我希望它对你有用:) 快乐编码:)
答案 1 :(得分:-1)
试试这个:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.more:
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.google.com"));
startActivity(browserIntent);
return true;
break;
default:
return super.onOptionsItemSelected(item);
}
}