问题描述
|
在许多应用程序中,您无法通过Android菜单访问首选项,设置和其他内容。
我希望能够链接到我创建的应用程序中的活动。
任何帮助和/或代码将不胜感激。
-EltMrx
解决方法
您必须重写Activity中的onOptionsItemSelected方法,当用户单击“选项”菜单中的项目时会调用该方法。在方法中,您可以检查单击了什么项目。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this,ActivityForItemOne.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity,this is for item with id \"menu_item2\"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
还有一个onContextItemSelected方法,其工作方式类似,但是对于Context菜单(我不确定,您指的是什么菜单)。
有关更多信息,请访问
http://developer.android.com/guide/topics/ui/menus.html
类意图-操作和类别常量http://developer.android.com/reference/android/content/Intent.html
动作元素
http://developer.android.com/guide/topics/manifest/action-element.html
意图和意图过滤器
http://developer.android.com/guide/topics/intents/intents-filters.html
希望这能解决您的问题。
致ICEMAN的信用,如何从Android中的菜单项调用“活动”?
, 这可能对您有帮助。