2014-05-19 143 views
0

我遇到了android webview默認文本選擇的問題。我想要做的就是添加一個項目到文本選擇顯示在網頁視圖Webview將菜單項添加到TextSelection菜單

I want to Add a Button to left of select all

我想acieve功能是添加一個按鈕來選擇的所有左側的默認菜單。如何才能做到這一點

回答

2

你需要重寫的WebView類這一點,你需要使用這個代碼我給你演示代碼以供選擇,你可以使用自己的上下文菜單後更改defaut CAB

public class MyWebView extends WebView{ 
CustomizedSelectActionModeCallback actionModeCallback; 
public Context context; 
public MyWebView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    // TODO Auto-generated constructor stub 
    this.context=context; 
} 
@Override 
public ActionMode startActionMode(ActionMode.Callback callback) { 
    // TODO Auto-generated method stub 
    //  ViewParent parent = getParent(); 
    //  if (parent == null) { 
    //   return null; 
    //  } 
    actionModeCallback = new CustomizedSelectActionModeCallback(); 
    return startActionModeForChild(this,actionModeCallback); 

} 

public class CustomizedSelectActionModeCallback implements ActionMode.Callback{ 

    @Override 
    public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
     // TODO Auto-generated method stub 
     mode.getMenuInflater().inflate(R.menu.contextual_menu, menu); 
     return true; 

    } 

    @Override 
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
     // TODO Auto-generated method stub 
     mode.setTitle("CheckBox is Checked"); 

     return false; 
    } 

    @Override 
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
     // TODO Auto-generated method stub 
     switch (item.getItemId()) { 
     case R.id.item_delete: 
      clearFocus(); 
      Toast.makeText(getContext(), "This is my test click", Toast.LENGTH_LONG).show(); 
      break; 

     default: 
      break; 
     } 
     return false; 
    } 

    @Override 
    public void onDestroyActionMode(ActionMode mode) { 
     // TODO Auto-generated method stub 


     clearFocus(); // this is not clearing the text in my device having version 4.1.2 
     actionModeCallback=null; 

    } 

} 

}

MainActivity.java

public class MainActivity extends Activity { 

MyWebView web_view; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    web_view= (MyWebView)findViewById(R.id.webView1); 
    web_view.loadUrl("file:///android_asset/menu_pages/Chemchapter1/OEBPS/Text/07_Chapter01.html");// you can load your html here 


} 

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="${packageName}.${activityClass}" > 


<com.rstm.webviewcheck.MyWebView 
    android:id="@+id/webView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

</RelativeLayout> 

contextual_menu.xml

<?xml version="1.0" encoding="utf-8"?> 

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
<item 

android:id="@+id/item_delete" 

android:icon="@android:drawable/ic_menu_delete" 

android:showAsAction="ifRoom|withText" 

android:title="Delete" 

android:titleCondensed="Delete"> 

image description

+1

感謝兄弟幫助了很多,並且工作完美:) JazakAllah –

+1

兄弟我們可以添加本地搜索按鈕到這個酒吧嗎?像它在默認情況下工作,我想要搜索功能工作。或者我應該爲它添加自定義代碼 –

+1

是啊!兄弟,你需要編寫自定義代碼,在自定義CAB中添加一個搜索按鈕並應用自定義代碼。 –

相關問題