Replace the default used webview with this findwebview, and rewrite the SelectedText class to show method string data is the selected text obtained
Import Android.annotation.SuppressLint;
Import Android.annotation.TargetApi;
Import Android.content.Context;
Import android.content.Intent;
Import Android.os.Build;
Import Android.text.TextUtils;
Import Android.util.AttributeSet;
Import Android.view.ActionMode;
Import Android.view.ActionMode.Callback;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import android.view.MotionEvent;
Import Android.webkit.JavascriptInterface;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
/**
* In the WebView interface to add long press the interface when the search button click the Search button to jump to the search page
*
* @author Wang Xunlong 360404113
* <p/>
* 2014-8-8 11:39:16
*/
@TargetApi (build.version_codes. Honeycomb)
public class Findwebview extends WebView {
Public Callback Callback;
Private Onscrollchangedlistener Monscrollchangedlistener;
@SuppressWarnings ("deprecation")
Public Findwebview (context context, AttributeSet attrs, int defstyle,
Boolean privatebrowsing) {
Super (context, Attrs, Defstyle, privatebrowsing);
Init ();
}
Public Findwebview (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
Init ();
}
Public Findwebview (context context, AttributeSet Attrs) {
Super (context, attrs);
Init ();
}
Public Findwebview (Context context) {
Super (context);
Init ();
}
@TargetApi (build.version_codes. KITKAT)
@SuppressLint ("setjavascriptenabled")
private void init () {
WebSettings setting = GetSettings ();
Setting.setjavascriptenabled (TRUE);
Setting.setjavascriptcanopenwindowsautomatically (TRUE);
Addjavascriptinterface (New SelectedText (), "search");
if (Build.VERSION.SDK_INT >= build.version_codes. KITKAT && constants.debug) {
Support WebView Debugging
Webview.setwebcontentsdebuggingenabled (TRUE);
}
}
@Override
Public Actionmode Startactionmode (Callback Callback) {
Customizedselectactionmodecallback customizedselectactionmodecallback = new Customizedselectactionmodecallback (
callback);
Return Super.startactionmode (Customizedselectactionmodecallback);
}
@Override
public boolean ontouchevent (Motionevent event) {
Return Super.ontouchevent (event);
}
public class Customizedselectactionmodecallback implements Actionmode.callback {
Private Callback Callback;
Public Customizedselectactionmodecallback (Callback Callback) {
This.callback = callback;
}
@Override
public boolean Oncreateactionmode (actionmode mode, menu menu) {
return Callback.oncreateactionmode (mode, menu);
}
@Override
public boolean Onprepareactionmode (actionmode mode, menu menu) {
return Callback.onprepareactionmode (mode, menu);
}
@Override
public boolean onactionitemclicked (actionmode mode, MenuItem item) {
if (item = = NULL | | Textutils.isempty (Item.gettitle ())) {
return callback.onactionitemclicked (mode, item);
}
if (!item.gettitle (). ToString (). Contains ("Search")
&&!item.gettitle (). ToString (). Contains ("search")) {
return callback.onactionitemclicked (mode, item);
}
Loadurl ("Javascript:window.search.show (Window.getselection (). toString ());");
Clearfocus ();
return true;
}
@Override
public void Ondestroyactionmode (Actionmode mode) {
Callback.ondestroyactionmode (mode);
}
}
public class SelectedText {
@JavascriptInterface
public void Show (String data) {
TODO here Gets the selected text
Intent Intent = new Intent (GetContext (), searchactivity.class);
Intent.putextra (searchactivity.tag_search, data);
GetContext (). StartActivity (Intent);
}
}
private int DY;
@Override
protected void onscrollchanged (int l, int t, int oldl, int Oldt) {
Super.onscrollchanged (L, T, OLDL, Oldt);
int dy = T-oldt;
dy + = dy;
if (Monscrollchangedlistener! = null && math.abs (DY) > 10) {
DY = 0;
Monscrollchangedlistener.onscroll (L, T, OLDL, Oldt);
}
}
Public interface Onscrollchangedlistener {
public void onscroll (int l, int t, int oldl, int oldt);
}
public void Setonscrollchangedlistener (Onscrollchangedlistener monscrollchangedlistener) {
This.monscrollchangedlistener = Monscrollchangedlistener;
}
}
Android rewrite WebView long time to select text and then click on the Search button event, default is Chrome accept Click event, now jump to 360 search page