Get the click element of Android WebView

Source: Internet
Author: User

Often see others in the app WebView long press the image can display large image, click the number can directly find the phone, then how this is achieved?

WebView's Gethittestresult () function can get the type of the clicked page element, and then we will do the corresponding processing according to the type.

1 hittestresult result = ((WebView) v). Gethittestresult ()
Hittestresult class
Method Describe
GetType () Gets the type of target selected, which can be a picture, hyperlink, mail, phone, etc.
Getextra () Get additional information

Specific use:

1Mwebview.setonlongclicklistener (NewOnlongclicklistener () {2 3              Public BooleanOnlongclick (View v) {4Hittestresult result =( (WebView) v). Gethittestresult ();5                 if(NULL==result)6                     return false;7                 intType =Result.gettype ();8                 Switch(type) {9                  CaseWebView.HitTestResult.EDIT_TEXT_TYPE://selected type of textTen                      Break; One                  CaseWebView.HitTestResult.PHONE_TYPE://Processing Dialing A                      Break; -                  CaseWebView.HitTestResult.EMAIL_TYPE://Handling Email -                      Break; the                  CaseWebView.HitTestResult.GEO_TYPE://Map Type -                      Break; -                  CaseWebView.HitTestResult.SRC_ANCHOR_TYPE://Hypertext Links -                      Break; +                  CaseWebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE://types of pictures with links -                  CaseWebView.HitTestResult.IMAGE_TYPE://working with long-pressed picture menu items} +                     return true; A                  CaseWebView.HitTestResult.UNKNOWN_TYPE://Unknown at                      Break; -                 } -                 return false; -             } -});

All enum type definitions are as follows:

  Type name

  Significance

  WebView.HitTestResult.UNKNOWN_TYPE

Unknown type

  WebView.HitTestResult.PHONE_TYPE

Phone type

  WebView.HitTestResult.EMAIL_TYPE

e-Mail type

  WebView.HitTestResult.GEO_TYPE

Map type

  WebView.HitTestResult.SRC_ANCHOR_TYPE

Type of hyperlink

  WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE

Types of pictures with links

  WebView.HitTestResult.IMAGE_TYPE

Simple type of picture

  WebView.HitTestResult.EDIT_TEXT_TYPE

Selected type of text

Get the click element of Android WebView

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.