本文介绍了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;
 }

我想有这样的菜单,我的Andr​​oid应用程序的每个活动和ListActivity共享。这对于具有在每个(目录)的活动,可以让用户跳转到内单击该应用程序的每一个部分标准菜单。

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.

现在,实现这一目标的最简单的方法是复制和粘贴在每一个(列表)活动应用程序的两种方法。我不喜欢这种冗余code写的:)

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;
   }    
}

然后,只需有你的活动扩展,而不是ListActivity MyListActivity。

Then just have your Activities extend MyListActivity instead of ListActivity.

我已经看到了子类化  我ListActivity的一个不工作  非常好(线程检索  从一个数据库对象是给  问题)。

这听起来像一个完全不同的问题。你可能会想张贴关于这一个单独的问题。简单地扩展一个类在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的:如何拥有,而无需重新编写的覆盖方法中的每个(列表)活动的共享菜单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:19