2013-05-03 100 views
2

我想刪除COPY,SELECT ALL從android上下文操作欄中找到並添加自定義菜單項。如何從上下文操作欄中刪除項目android

enter image description here

而上的WebView選擇文本,這是出現的。我正在嘗試在使用js的webview上添加文本亮點。

+0

爲什麼你不使用第三方庫來回動作吧? – 2013-05-07 05:04:22

+0

你有沒有檢查操作欄sherlock? – 2013-05-07 05:06:20

+0

我是新來的機器人。請建議那些圖書館。 – Ashini 2013-05-07 05:07:28

回答

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搜索。不幸的是,這個方法確實需要你自己實現所有的功能。但是,您可以深入瞭解這些功能的源代碼(我將從 ActionMode.java開始)。在 CustomActionModeCallback.onActionItemClicked(處理按鈕事件的地方)中實施新案例,從源代碼複製/粘貼功能,並在XML文件中添加相應的按鈕。您甚至可以使用本地圖標執行以下功能: android:icon="@android:drawable/[name_of_desired_icon]

僅供參考,此信息來自Android開發人員網站。
http://developer.android.com/guide/topics/ui/menus.html#CAB

相關問題