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

Android 如何从上下文操作栏中删除项目,android,contextual-action-bar,Android,Contextual Action Bar,我想删除复制,从android上下文操作栏中选择ALL和FIND,并添加自定义菜单项 这是在webview上选择文本时出现的。我正在尝试使用js在webview上添加文本突出显示。这可能对您和堆栈成员有所帮助。。。 为了实现您想要的功能,您需要创建一个全新的上下文操作栏。这是通过创建自定义的操作模式来实现的。在WebView中,创建一个实现ActionMode.Callback的嵌套类。您可以将其用作模板: public class CustomWebView extends WebView

我想删除复制,从android上下文操作栏中选择ALLFIND,并添加自定义菜单项


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

这可能对您和堆栈成员有所帮助。。。

为了实现您想要的功能,您需要创建一个全新的上下文操作栏。这是通过创建自定义的
操作模式来实现的。在
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;
        }
    }

}
<!-- 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>

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

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;
        }
    }

}
<!-- 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>


我注意到您没有明确声明要替换共享和WEB搜索。不幸的是,这种方法确实需要您自己实现所有功能。但是,您可以深入研究这些函数的源代码(我将从
ActionMode.java
开始)。在
CustomActionModeCallback.onActionItemClicked
(处理按钮事件的地方)中实现一个新案例,从源代码复制/粘贴功能,并在XML文件中添加相应的按钮。您甚至可以将本机图标用于这些功能:
android:icon=“@android:drawable/[name\u of\u desired\u icon]

作为参考,此信息来自Android开发者网站。

为什么不在操作栏上使用第三方库?你有没有检查过操作栏sherlock?我是android新手。请建议在webview上选择文本时出现那些库。CAB。如何禁用它。你的应用程序android:minSdkVersion??