在 Android 上实现应用程序范围选项菜单的最佳方法是什么?

发布于 2024-12-15 04:42:16 字数 469 浏览 0 评论 0原文

我是一名 Android 新手,正在开发一个需要选项菜单的应用程序。我目前通过将选项菜单设置为主要活动并在主要活动中扩展它来实现选项菜单。

但由于我在团队中工作,这种方法并不总是适用于我们,因为我们需要扩展另一项重要的活动。

我的问题 如何在整个应用程序中实现此选项菜单而不扩展主活动中的活动?

我当前的设置

  1. 我有一个 MainActivity (首先启动) - MainActivity 扩展了 MenuClass
  2. 我有 OptionsMenu 类,MenuClass (我希望这是应用程序范围内的) - MenuClass 扩展了 Activity
  3. 我还有其他三个类,即扩展了 Activity 本身!这三个活动都是从MainActivity触发的,完成后返回到MainActivity。

I'm an Android newbie, and working on an app which needs an Options menu. I have currently implemented the options menu by setting it as the primary activity and Extending it in the main activity.

But since I work in a team, this method doesn't work always with us since we need to extend another activity which is essential.

My Question
How do I implement this Options Menu across the application without Extending the activity in my Main activity?

My Current Setup

  1. I have a MainActivity (This starts first) - MainActivity extends MenuClass
  2. I have the OptionsMenu Class, MenuClass (I want this to be Application wide) - MenuClass extends Activity
  3. I have three other Classes, that extends Activity itself! And these three activities are triggered from the MainActivity and when done, returns to the MainActivity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晌融 2024-12-22 04:42:16

如果您不想或无法创建一个基础 Activity,然后每个其他 Activity 都会对其进行扩展 - 为什么您没有一个具有 public static void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 的实用程序类? ...} 函数和 public static boolean onOptionsItemSelected(MenuItem item) {...}

public class Utils {
    public static void onCreateOptionsMenu(Menu menu, MenuInflater inflater ){
        //... create default options here
    }

    public static boolean onOptionsItemSelected(MenuItem item) {
        //... see if you want to handle the selected option here, return true if handled
    }
  }

然后从您的 Activity 中您可以执行以下操作:

public class YourActivity extends Activity {

// ...


public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ){
   Utils.onOptionsItemSelected(menu, inflater);
   //... add other options here
}


public boolean onOptionsItemSelected(MenuItem item) {
    boolean handled = Utils.onOptionsItemSelected(item);
    if (!handled) {
        switch(item.getItemId()) {
            case R.id.menu_sign_out:
                //... deal with option
            break;
            //.. deal with other options
        }
    }
    return handled;
}

您可能希望根据您将其构建到应用程序中的方式来更改其具体实现 - 即您可能不希望 utils 方法是静态的,因为您可能需要维护某些状态在那里,但这应该有效。

If you don't want to, or can't create a base Activity which every other activity then extends - why don't you have a utilities class which has a public static void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {...} function and a public static boolean onOptionsItemSelected(MenuItem item) {...}?

public class Utils {
    public static void onCreateOptionsMenu(Menu menu, MenuInflater inflater ){
        //... create default options here
    }

    public static boolean onOptionsItemSelected(MenuItem item) {
        //... see if you want to handle the selected option here, return true if handled
    }
  }

then from you Activity you can do this:

public class YourActivity extends Activity {

// ...


public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ){
   Utils.onOptionsItemSelected(menu, inflater);
   //... add other options here
}


public boolean onOptionsItemSelected(MenuItem item) {
    boolean handled = Utils.onOptionsItemSelected(item);
    if (!handled) {
        switch(item.getItemId()) {
            case R.id.menu_sign_out:
                //... deal with option
            break;
            //.. deal with other options
        }
    }
    return handled;
}

You may want to change the exact implementation of this depending on how you build it in to your app - ie you may not want the utils methods to be static as you may require some state to be maintained in there, but this should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文