如何调用 ActionBar 的类似 ContextMenu 的行为?

发布于 2024-11-10 06:14:58 字数 176 浏览 2 评论 0 原文

例如,在 Android 3.0 中,当您选择某些文本时,ActionBar 会切换到类似 ContextMenu 的模式,使您能够对所选文本执行操作:复制/共享等,并且左侧会出现“完成”按钮使用户能够离开此模式。

如何在我的应用程序中将 ActionBar 切换到此模式(当然还有我的菜单项)?我只是在文档中找不到这个。

In Android 3.0, when you select some text for example, the ActionBar switches to a ContextMenu-like mode, which enables you to do actions with the selected text: copy/share/etc, and a "Done" button appears on the left side to enable the user to leave this mode.

How can I switch the ActionBar into this mode in my app (with my menu items of course)? I just couldn't find this in the docs.

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

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

发布评论

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

评论(3

奈何桥上唱咆哮 2024-11-17 06:14:59

在此处输入图像描述

使用新的 上下文操作栏,请参阅“为各个视图启用上下文操作模式”。
它指出:

如果您只想在用户选择特定选项时调用上下文操作模式
视图,您应该:

  1. 实现 ActionMode.Callback接口。在它的回调方法中,你
    可以指定上下文操作栏的操作、响应操作项上的单击事件以及处理操作模式的其他生命周期事件。
  2. 调用 startActionMode()< /a> 当你想显示
    栏(例如当用户长按视图时)。

例如:

  1. 实现 ActionMode.Callback接口:
    <前><代码>
    私有 ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    // 创建动作模式时调用; startActionMode() 被调用
    @覆盖
    公共布尔onCreateActionMode(ActionMode模式,菜单菜单){
    // 膨胀提供上下文菜单项的菜单资源
    MenuInflater inflater = mode.getMenuInflater();
    inflater.inflate(R.menu.context_menu, 菜单);
    返回真;
    }

    // 每次显示操作模式时调用。总是在 onCreateActionMode 之后调用,但是
    // 如果模式无效,可能会被多次调用。
    @覆盖
    public boolean onPrepareActionMode(ActionMode 模式,Menu 菜单) {
    返回假; // 如果什么都不做则返回 false
    }

    // 当用户选择上下文菜单项时调用
    @覆盖
    public boolean onActionItemClicked(ActionMode 模式, MenuItem 项) {
    开关 (item.getItemId()) {
    案例 R.id.menu_share:
    分享当前项目();
    模式.finish(); // 选择操作,因此关闭 CAB
    返回真;
    默认:
    返回假;
    }
    }

    // 当用户退出操作模式时调用
    @覆盖
    公共无效onDestroyActionMode(ActionMode模式){
    mActionMode = null;
    }
    };

    请注意,这些事件回调几乎与选项菜单的回调完全相同,除了每个回调还传递 ActionMode 与事件关联的对象。您可以使用 ActionMode API 来制作对 CAB 进行各种更改,例如修改标题和
    副标题 setTitle()setSubtitle()< /code> (用于指示有多少件物品
    已选择)。

    另请注意,上面的示例在以下情况下将 mActionMode 变量设置为 null:
    行动模式被破坏。在下一步中,您将看到它是如何初始化以及如何保存的
    您的活动或片段中的成员变量可能很有用。

  2. 调用 startActionMode()< /a> 启用上下文
    适当时的操作模式,例如响应长按
    查看

    <前><代码>
    someView.setOnLongClickListener(new View.OnLongClickListener() {
    // 当用户长按 someView 时调用
    公共布尔onLongClick(查看视图){
    if (mActionMode != null) {
    返回假;
    }

    // 使用上面定义的 ActionMode.Callback 启动 CAB
    mActionMode = getActivity().startActionMode(mActionModeCallback);
    view.setSelected(true);
    返回真;
    }
    });

    当您调用 startActionMode(),系统返回
    创建的 ActionMode。通过将其保存在成员变量中,您可以
    更改上下文操作栏以响应其他事件。在上面的示例中,
    ActionMode 用于确保 < code>ActionMode 实例
    如果它已经处于活动状态,则不会重新创建,方法是在启动之前检查成员是否为空
    动作模式。

Enabling batch contextual actions in a ListView or GridView

如果您在 ListViewGridView (或其他扩展<代码>AbsListView)并想要
允许用户执行批量操作,您应该:

例如:

ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                          long id, boolean checked) {
        // Here you can do something when items are selected/de-selected,
        // such as update the title in the CAB
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // Respond to clicks on the actions in the CAB
        switch (item.getItemId()) {
            case R.id.menu_delete:
                deleteSelectedItems();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the menu for the CAB
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // Here you can make any necessary updates to the activity when
        // the CAB is removed. By default, selected items are deselected/unchecked.
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // Here you can perform updates to the CAB due to
        // an invalidate() request
        return false;
    }
});

就是这样。现在,当用户长按选择一个项目时,系统会调用 onCreateActionMode()
方法并显示具有指定操作的上下文操作栏。虽然上下文
操作栏可见,用户可以选择其他项目。

在某些情况下,上下文操作提供通用操作项,您可能
想要添加一个复选框或类似的 UI 元素来允许用户选择项目,因为他们
可能无法发现长按行为。当用户选择该复选框时,您
可以通过将相应的列表项设置为选中来调用上下文操作模式
使用 setItemChecked()

enter image description here

To use the new contextual action bar, see "Enabling the contextual action mode for individual views".
It states:

If you want to invoke the contextual action mode only when the user selects specific
views, you should:

  1. Implement the ActionMode.Callback interface. In its callback methods, you
    can specify the actions for the contextual action bar, respond to click events on action items, and handle other lifecycle events for the action mode.
  2. Call startActionMode() when you want to show the
    bar (such as when the user long-clicks the view).

For example:

  1. Implement the ActionMode.Callback interface:
    
    private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
    
        // Called when the action mode is created; startActionMode() was called
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate a menu resource providing context menu items
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            return true;
        }
    
        // Called each time the action mode is shown. Always called after onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false; // Return false if nothing is done
        }
    
        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_share:
                    shareCurrentItem();
                    mode.finish(); // Action picked, so close the CAB
                    return true;
                default:
                    return false;
            }
        }
    
        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionMode = null;
        }
    };
    
    

    Notice that these event callbacks are almost exactly the same as the callbacks for the options menu, except each of these also pass the ActionMode object associated with the event. You can use ActionMode APIs to make various changes to the CAB, such as revise the title and
    subtitle with setTitle() and setSubtitle() (useful to indicate how many items are
    selected).

    Also notice that the above sample sets the mActionMode variable null when the
    action mode is destroyed. In the next step, you'll see how it's initialized and how saving
    the member variable in your activity or fragment can be useful.

  2. Call startActionMode() to enable the contextual
    action mode when appropriate, such as in response to a long-click on a View:

    
    someView.setOnLongClickListener(new View.OnLongClickListener() {
        // Called when the user long-clicks on someView
        public boolean onLongClick(View view) {
            if (mActionMode != null) {
                return false;
            }
    
            // Start the CAB using the ActionMode.Callback defined above
            mActionMode = getActivity().startActionMode(mActionModeCallback);
            view.setSelected(true);
            return true;
        }
    });
    
    

    When you call startActionMode(), the system returns
    the ActionMode created. By saving this in a member variable, you can
    make changes to the contextual action bar in response to other events. In the above sample, the
    ActionMode is used to ensure that the ActionMode instance
    is not recreated if it's already active, by checking whether the member is null before starting the
    action mode.

Enabling batch contextual actions in a ListView or GridView

If you have a collection of items in a ListView or GridView (or another extension of AbsListView) and want to
allow users to perform batch actions, you should:

For example:

ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                          long id, boolean checked) {
        // Here you can do something when items are selected/de-selected,
        // such as update the title in the CAB
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // Respond to clicks on the actions in the CAB
        switch (item.getItemId()) {
            case R.id.menu_delete:
                deleteSelectedItems();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the menu for the CAB
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // Here you can make any necessary updates to the activity when
        // the CAB is removed. By default, selected items are deselected/unchecked.
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // Here you can perform updates to the CAB due to
        // an invalidate() request
        return false;
    }
});

That's it. Now when the user selects an item with a long-click, the system calls the onCreateActionMode()
method and displays the contextual action bar with the specified actions. While the contextual
action bar is visible, users can select additional items.

In some cases in which the contextual actions provide common action items, you might
want to add a checkbox or a similar UI element that allows users to select items, because they
might not discover the long-click behavior. When a user selects the checkbox, you
can invoke the contextual action mode by setting the respective list item to the checked
state with setItemChecked().

娇柔作态 2024-11-17 06:14:59

是的,我也找不到它——我不得不在 Google I|O 上询问。

使用 startActionMode() 这是他们的示例之一 这证明了这一点。我自己需要在这方面做更多的工作。

Yeah, I couldn't find it either -- I had to ask at Google I|O.

Use startActionMode(). Here is one of their samples that demonstrates it. I need to do more work in this area myself.

小糖芽 2024-11-17 06:14:59

也许有点晚了,但这里有一个关于动作模式的教程:
http://www.vogella.com/articles/AndroidListView/article.html#listview_actionbar

Maybe a bit late but here's a tutorial for the actionmode:
http://www.vogella.com/articles/AndroidListView/article.html#listview_actionbar

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