我想刪除COPY,SELECT ALL和從android上下文操作欄中找到並添加自定義菜單項。如何從上下文操作欄中刪除項目android
而上的WebView選擇文本,這是出現的。我正在嘗試在使用js的webview上添加文本亮點。
我想刪除COPY,SELECT ALL和從android上下文操作欄中找到並添加自定義菜單項。如何從上下文操作欄中刪除項目android
而上的WebView選擇文本,這是出現的。我正在嘗試在使用js的webview上添加文本亮點。
可能這可以幫助你,也堆疊成員... https://github.com/btate/BTAndroidWebViewSelection
我試過了。在那個來源中,他們使用自定義的webview。 – Ashini 2013-05-07 07:00:28
爲了完成你想要什麼,你需要創建一個全新的上下文操作欄。這是通過創建自定義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;
}
}
}
<!-- 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>
ActionMode.java
開始)。在
CustomActionModeCallback.onActionItemClicked
(處理按鈕事件的地方)中實施新案例,從源代碼複製/粘貼功能,並在XML文件中添加相應的按鈕。您甚至可以使用本地圖標執行以下功能:
android:icon="@android:drawable/[name_of_desired_icon]
僅供參考,此信息來自Android開發人員網站。
http://developer.android.com/guide/topics/ui/menus.html#CAB
爲什麼你不使用第三方庫來回動作吧? – 2013-05-07 05:04:22
你有沒有檢查操作欄sherlock? – 2013-05-07 05:06:20
我是新來的機器人。請建議那些圖書館。 – Ashini 2013-05-07 05:07:28