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

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.