我想从Android上下文操作栏中删除 COPY,SELECT ALL 和 FIND ,然后添加自定义菜单项。
在webview上选择文本时出现。我正在尝试使用js在webview上添加文本突出显示。
答案 0 :(得分: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>
<小时/> 我注意到您没有明确声明要替换分享和 WEB 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
答案 1 :(得分:0)
这可能对你有所帮助,也可以堆叠成员......
https://github.com/btate/BTAndroidWebViewSelection