将相同的上下文菜单添加到多个活动
我试图弄清楚如何在多个活动中包含常见的代码片段。
更具体地说,我有一个上下文菜单,我想将其包含在多个活动中。 我看到了这个,但只是不明白如何扩展到多个活动。 http://developer.android.com/guide/topics/ui/menus.html
我将其设置为 Menu.java
public class Menu extends Activity{
// bottom menus
public static final int Menu1 = 1;
public static final int Menu2 = 2;
public static final int Menu3 = 3;
public static final int Menu4 = 4;
public static final int Menu5 = 5;
public static final int Menu6 = 6;
public static final int Menu7 = 7;
// / Creates the menu items
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, Menu3, 0, "Create Profile").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_add));
menu.add(0, Menu5, 0, "Log In").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_login));
menu.add(0, Menu2, 0, "Settings").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_preferences));
menu.add(0, Menu4, 0, "About").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_help));
menu.add(0, Menu1, 0, "Report A Bug").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_start_conversation));
menu.add(0, Menu6, 0, "New Stuff").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_view));
return true;
}
private MenuItem add(int i, int menu32, int j, String string) {
// TODO Auto-generated method stub
return null;
}
// Handles item selections from preference menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu1:
startActivity(new Intent(this, Bug.class));
return true;
case Menu2:
startActivity(new Intent(this, EditPreferences.class));
return true;
case Menu3:
startActivity(new Intent(this, CreateAccount.class));
return true;
case Menu4:
startActivity(new Intent(this, About.class));
return true;
case Menu5:
startActivity(new Intent(this, Login.class));
return true;
case Menu6:
startActivity(new Intent(this, NewAdditions.class));
return true;
}
return false;
}
}
I'm trying to figure out how to include common pieces of code in multiple activities.
More specifically, I have a context menu that I would like to include in several activities.
I saw this, but just don't understand how to extend to multiple activities.
http://developer.android.com/guide/topics/ui/menus.html
I have this set up as Menu.java
public class Menu extends Activity{
// bottom menus
public static final int Menu1 = 1;
public static final int Menu2 = 2;
public static final int Menu3 = 3;
public static final int Menu4 = 4;
public static final int Menu5 = 5;
public static final int Menu6 = 6;
public static final int Menu7 = 7;
// / Creates the menu items
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, Menu3, 0, "Create Profile").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_add));
menu.add(0, Menu5, 0, "Log In").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_login));
menu.add(0, Menu2, 0, "Settings").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_preferences));
menu.add(0, Menu4, 0, "About").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_help));
menu.add(0, Menu1, 0, "Report A Bug").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_start_conversation));
menu.add(0, Menu6, 0, "New Stuff").setIcon(
this.getResources().getDrawable(R.drawable.ic_menu_view));
return true;
}
private MenuItem add(int i, int menu32, int j, String string) {
// TODO Auto-generated method stub
return null;
}
// Handles item selections from preference menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu1:
startActivity(new Intent(this, Bug.class));
return true;
case Menu2:
startActivity(new Intent(this, EditPreferences.class));
return true;
case Menu3:
startActivity(new Intent(this, CreateAccount.class));
return true;
case Menu4:
startActivity(new Intent(this, About.class));
return true;
case Menu5:
startActivity(new Intent(this, Login.class));
return true;
case Menu6:
startActivity(new Intent(this, NewAdditions.class));
return true;
}
return false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想在超过 1 个活动中添加相同的功能,而不是创建 1 个通用活动
像 BaseActivity 一样,扩展该活动将包括您继承的所有活动中的常用功能,
例如我调用了 checklogin 函数,您可以将菜单代码放在这里,
现在您可以在您的活动中扩展它
if you want to add same functionality in more than 1 activity than create 1 common activity
like BaseActivity and extend that activity will include that common functions in your inherited all activities
for example i have called checklogin function , you can put your menu code here,
now you can extend it in your activities
您可以在 xml 文件中定义菜单,然后在 onCreateOptionsMenu 中加载该菜单。您仍然需要处理每个活动中的每个菜单项。您还可以创建一个 BaseActivity 类来处理每个 Activity 可以扩展的菜单内容。
You can define a menu in an xml file and then load the menu in onCreateOptionsMenu. You will still need to handle each menu item in each activity. You could also create a BaseActivity class that handles the menu stuff that each Activity could extend.
尝试使用
abstract
类现在扩展类
BaseMenu
而不是Activity
我认为这可以帮助您。
Try to use a
abstract
classNow extend the class
BaseMenu
instead ofActivity
I Think this could help you out.