Android:如何在不重写重写方法的情况下在每个(列表)活动中有一个共享菜单? [英] Android: How to have a shared menu in each (List) Activity without re-writing the overridden methods?

查看:12
本文介绍了Android:如何在不重写重写方法的情况下在每个(列表)活动中有一个共享菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Android 提供了一些有用的方法来定义菜单:

I know that Android provides some useful methods to be overridden in order to define a menu:

    @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  menu.add(0, AIS, 0, "Activity Inventory Sheet").setIcon(android.R.drawable.ic_menu_upload);
                // ...
  return true;
 }

 public boolean onOptionsItemSelected(MenuItem item) {
  Intent i;
     switch (item.getItemId()) {
     case AIS: i = new Intent(this, ActivityInventorySheet.class);
      startActivity(i);
               return true;
     // ...
     }
     return false;
 }

我想让我的 Android 应用程序的每个 Activity 和 ListActivity 共享此菜单.这是为了在每个(列表)Activity 中都有一个标准菜单,让用户可以通过单击跳转到应用程序的每个部分.

I would like to have this menu shared by each Activity and ListActivity of my Android application. This is for having a standard menu in each (List) Activity that lets the user jump to every part of the application within a click.

现在,实现此目标的最简单方法是在应用程序的每个(列表)活动中复制并粘贴这两种方法.我不喜欢这种冗长的代码编写 :)

Right now, the easiest way to achieve this is to copy-and-paste both methods in every (List) Activity of the application. I don't like this redundancy of code written :)

子分类是一个合理的选择吗?我已经看到对我的 ListActivity 之一进行子类化效果不佳(从数据库中检索对象的线程出现问题).还有其他方法可以通过活动共享菜单吗?

Is sub-classing a reasonable choice? I've already seen that sub-classing one of my ListActivity does not work very well (threads that retrieve objects from a database are giving problems). Are there other ways to share a menu though Activities?

谢谢

推荐答案

我认为没有理由这不能完美地工作:

I see no reason this wouldn't work perfectly:

public abstract class MyListActivity extends ListActivity
{
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      menu.add(0, AIS, 0, "Activity Inventory Sheet").setIcon(android.R.drawable.ic_menu_upload);
      // ...
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      Intent i;
      switch (item.getItemId()) {
          case AIS: i = new Intent(this, ActivityInventorySheet.class);
          startActivity(i);
          return true;
          // ...
      }
      return false;
   }    
}

然后让您的活动扩展 MyListActivity 而不是 ListActivity.

Then just have your Activities extend MyListActivity instead of ListActivity.

我已经看到了子分类我的 ListActivity 之一不起作用很好(检索的线程来自数据库的对象给出问题).

I've already seen that sub-classing one of my ListActivity does not work very well (threads that retrieve objects from a database are giving problems).

这听起来像是一个完全不同的问题.您可能想就此发布一个单独的问题.简单地在 Java 中扩展一个类不应该产生像您所描述的那样的任何问题.

This sounds like a completely different problem. You might want to post a separate question regarding this. Simply extending a class in Java shouldn't create any problems like the one you are describing.

这篇关于Android:如何在不重写重写方法的情况下在每个(列表)活动中有一个共享菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆