2

我想从 android 上下文操作栏中删除COPY、SELECT ALLFIND并添加自定义菜单项。

在此处输入图像描述

这是在 webview 上选择文本时出现的。我正在尝试使用 js 在 webview 上添加文本突出显示。

4

2 回答 2

2

为了完成你想要的,你需要创建一个全新的上下文操作栏。这是通过创建自定义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 文件中添加相应的按钮。您甚至可以将本机图标用于这些功能:android:icon="@android:drawable/[name_of_desired_icon]

作为参考,此信息来自 Android 开发者网站。
http://developer.android.com/guide/topics/ui/menus.html#CAB

于 2014-04-16T14:38:26.003 回答
0

可能这对您和堆叠成员都有帮助... https://github.com/btate/BTAndroidWebViewSelection

于 2013-05-07T06:58:04.613 回答