android - 如何从上下文操作栏中删除项目android

标签 android contextual-action-bar

我想从 Android 上下文操作栏中删除复制、全选查找并添加自定义菜单项。

enter image description here

在 WebView 上选择文本时会出现此情况。我正在尝试使用 js 在 webview 上添加文本突出显示。

最佳答案

为了完成您想要的任务,您需要创建一个全新的上下文操作栏。这是通过创建自定义 ActionMode 来完成的。在您的 WebView 中,创建一个实现 ActionMode.Callback 的嵌套类。您可以使用它作为模板:

public class CustomWebView extends WebView {

    private ActionMode.Callback mActionModeCallback;

    @Override
    public ActionMode startActionMode(ActionMode mode) {
        // This block is directly from the WebView source code.
        ViewParent parent = getParent();
        if (parent == null) {
            return null;
        }

        mActionModeCallback = new CustomActionModeCallback();
        return parent.startActionModeForChild(this, mActionModeCallback);
    }

    private class CustomActionModeCallback implements 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) {
            // This method is called when the selection handlebars are moved.
            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_button_1:
                // Do stuff
                break;
            case R.id.menu_button_2:
                // Do stuff
                break;
            default:
                // You did not handle the action, so return false
                // If you have implemented a case for every button,
                // this block should never be called.
                return false;
            }

            // If you want to close the CAB immediately after 
            // picking an action, call mode.finish().
            // If you want the CAB to persist until the user clears the selection
            // or clicks the "Done" button, simply return true.
            mode.finish(); // Action picked, so close the CAB
            return true;
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mode = null;
        }
    }

}


请务必在 XML 资源中定义菜单。以下是使用上述模板的示例:

<!-- context_menu.xml -->
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menu_button_1"
        android:icon="@android:drawable/menu_button_1"
        android:showAsAction="always"
        android:title="@string/menu_button_1">
    </item>
    <item
        android:id="@+id/menu_button_2"
        android:icon="@drawable/menu_button_2"
        android:showAsAction="ifRoom"
        android:title="@string/menu_button_2">
    </item>

</menu>


我注意到您没有明确表示要替换 SHAREWEB SEARCH。不幸的是,这种方法确实需要您自己实现所有功能。但是,您可以深入研究这些函数的源代码(我将从 ActionMode.java 开始)。在 CustomActionModeCallback.onActionItemClicked(处理按钮事件的位置)中实现一个新案例,从源代码复制/粘贴功能,然后在 XML 文件中添加相应的按钮。您甚至可以使用 native 图标来实现这些功能:android:icon="@android:drawable/[name_of_desired_icon]

此信息来自 Android 开发者网站,仅供引用。
http://developer.android.com/guide/topics/ui/menus.html#CAB

关于android - 如何从上下文操作栏中删除项目android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16356102/

相关文章:

android - Android 中 "DONE"按钮的上下文操作栏文本更改

java - 带有 Junit 4.7 "!!! JUnit version 3.8 or later expected:"的 IntelliJ IDEA

android - 用于显示文本选择句柄的 EditText 的自定义剪切/复制操作栏

android - 如何突出显示或检查选定的列表项

java - 如何在 strings.xml 或 asset 中存储数组并在 ListViewAdapter 中使用

android - OnItemLongClickListener 在自定义 ListView 中不起作用

java - ListView OnItemLongClickListener() 未触发

Android ScrollView 在旋转动画中丢失剪辑

android - 基本 fragment 布局的困难

android - 如何从 url 保存图像?