使用 menu.addIntentOptions() 时自定义菜单选项

发布于 2024-10-11 00:46:30 字数 3636 浏览 3 评论 0原文

我正在构建一个基于 Google 提供的记事本应用程序的简单应用程序。我的第一步是在可能的情况下将应用程序转换为使用 XML 菜单。在主要活动(笔记列表)中,我使用 MenuInflater 显示默认的“撰写”菜单选项:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // menu initialization, use the baseline menu from XML
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.noteslist, menu);

    // generate any additional actions that can be performed on the
    // overall list.  In a normal install, there are no additional
    // actions found here, but this allows other applications to extend
    // our menu with their own actions.
    Intent intent = new Intent(null, getIntent().getData());
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, new ComponentName(this, NotesList.class), null, intent, 0, null);

    return true;
}

使用noteslist.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu 
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/compose_note"
      android:icon="@drawable/ic_menu_compose"
      android:title="@string/menu_compose"
      android:alphabeticShortcut="c"
      android:numericShortcut="3" />
</menu>

一切正常。现在,根据示例(并进行了修改,因为示例尝试在列表中没有选择任何项目时添加意图选项),如果列表中有项目并且选择了其中之一,我想添加一些附加选项:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    super.onPrepareOptionsMenu(menu);

    // determine if we have any items in the list via the ListAdapter
    final boolean haveItems = (getListAdapter().getCount() > 0);

    // do we have items?
    if (haveItems) {
        // there are items, check if any are selected
        //Toast.makeText(getApplicationContext(), "position: " + getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
        if (getSelectedItemPosition() >= 0) {
            // an item is selected, add the intents for one of our list items to the menu
            Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());

            // build menu on the fly... always starts with the EDIT action
            Intent[] specifics = new Intent[1];
            specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
            MenuItem[] items = new MenuItem[1];

            // now add additional CATEGORY_ALTERNATIVE intent-based actions, (see the manifest)
            Intent intent = new Intent(null, uri);
            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
            menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);

            // finally, add a shortcut to the edit menu item
            if (items[0] != null) {
                items[0].setShortcut('1', 'e');
            }
        }
    } else {
        menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
    }

    return true;
}

最后,引用的 AndroidManifest.xml,与演示应用程序的默认值相同:

http ://developer.android.com/resources/samples/NotePad/AndroidManifest.html

所以,我有两个问题:

1)当项目被选中时,由 onPrepareOptionsMenu() 生成的结果菜单,编辑注释和编辑标题选择后,使用默认图标并且没有分配快捷方式。我可以通过 android:icon="" 将意图过滤器设置为具有不同的图标,但是分配字母和数字快捷方式就没那么幸运了...我想指定这些,并希望可能有一种方法在 XML 中定义这些菜单项,并且当应用程序通过意图过滤器识别将它们引入时,还需要提取 XML 并以某种方式膨胀/导入它。有什么建议或指示吗?

2)在 onCreateOptionsMenu() 中,为什么使用 CATEGORY_ALTERNATIVE 的 addIntentOptions() 调用没有将意图过滤器设置为category.ALTERNATIVE 的活动添加到菜单中(在这种情况下不添加是正确的行为,只是试图让我了解如何onCreateOptionsMenu() 和 onPrepareOptionsMenu() 中对 addIntentOptions() 的调用几乎相同,会导致不同的菜单)。

I am building a simple application based off of the Google provided Note pad app. One of my first steps is converting the app to use XML menus, where possible. In the main activity, the notes list, I am using MenuInflater to show the default 'Compose' menu option:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    // menu initialization, use the baseline menu from XML
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.noteslist, menu);

    // generate any additional actions that can be performed on the
    // overall list.  In a normal install, there are no additional
    // actions found here, but this allows other applications to extend
    // our menu with their own actions.
    Intent intent = new Intent(null, getIntent().getData());
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, new ComponentName(this, NotesList.class), null, intent, 0, null);

    return true;
}

With noteslist.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu 
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/compose_note"
      android:icon="@drawable/ic_menu_compose"
      android:title="@string/menu_compose"
      android:alphabeticShortcut="c"
      android:numericShortcut="3" />
</menu>

Everything works fine. Now, per the example (and modified as the example tries to add intent options when there are no items selected on the list as well), I want to add some additional options if there are items in the list, and one of them is selected:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    super.onPrepareOptionsMenu(menu);

    // determine if we have any items in the list via the ListAdapter
    final boolean haveItems = (getListAdapter().getCount() > 0);

    // do we have items?
    if (haveItems) {
        // there are items, check if any are selected
        //Toast.makeText(getApplicationContext(), "position: " + getSelectedItemPosition(), Toast.LENGTH_SHORT).show();
        if (getSelectedItemPosition() >= 0) {
            // an item is selected, add the intents for one of our list items to the menu
            Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());

            // build menu on the fly... always starts with the EDIT action
            Intent[] specifics = new Intent[1];
            specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
            MenuItem[] items = new MenuItem[1];

            // now add additional CATEGORY_ALTERNATIVE intent-based actions, (see the manifest)
            Intent intent = new Intent(null, uri);
            intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
            menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);

            // finally, add a shortcut to the edit menu item
            if (items[0] != null) {
                items[0].setShortcut('1', 'e');
            }
        }
    } else {
        menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
    }

    return true;
}

Finally, the referenced AndroidManifest.xml, unchanged from the default for the demo app:

http://developer.android.com/resources/samples/NotePad/AndroidManifest.html

So, I have two questions:

1) The resulting menus, Edit Note and Edit Title, generated by onPrepareOptionsMenu() when an item is selected, use the default icon and have no shortcuts assigned. I can set the intent-filter to have a different icon via android:icon="", but no such luck with assigning alphabetic and numeric shortcuts... I'd like to specify these, and was hoping that there might be a way to define these menu items in XML, and when they are to be brought in by the app via being identified by the intent-filters, also pull the XML and inflate/import it somehow. Any suggestions or pointers?

2) In onCreateOptionsMenu(), why is the addIntentOptions() call with CATEGORY_ALTERNATIVE NOT adding the activities with intent-filter set to category.ALTERNATIVE to the menu (not adding is correct behavior in this case, just trying to get my head around how the virtually identical calls to addIntentOptions() in onCreateOptionsMenu() and onPrepareOptionsMenu() result in different menus).

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

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

发布评论

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

评论(1

彻夜缠绵 2024-10-18 00:46:30

为了解决这个问题,我将根据上述评论发布 CommonsWare 的解决方案,即本质上,由于菜单污染,Android 开发已经不再使用 Intents 来生成菜单项,如上所示 线程

“...我们放弃了这种方法因为管理任意数量的附加项目的 UI 是一个挑战......”

In an effort to close this question, I'll post CommonsWare's solution from the above comments, which is that in essence, Android development has moved away from using Intents to generate menu items due to menu pollution, as indicated on this thread from a member of the Android team:

"...We moved away from this approach because managing the UI for an arbitrary number of additional items is a challenge..."

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