Android webview for Text Selection

Source: Internet
Author: User

Http://www.androidlearner.net/android-webview-text-selection.html

Recently, I am working on an application. In webview, You need to enable the pop-up menu after long-clicking to enter the text selection mode. I found a piece of available onlineCodeIn fact, after android4.0, the system comes with the webview's long-Text Selection Function.

12345678910111213141516171819 /** * Implements webview Text Selection. After Android, the default value is "Long press" to enter the text selection mode. */ Private
Void Entertextselection (){ If
(Android. OS. Build. version. sdk_int> = build. version_codes.gingerbread_mr1)
Return ; Try
{
Webview. Class . Getmethod ( "Selecttext" ). Invoke ( This );
} Catch
(Exception e ){

Try {
Webview. Class . Getmethod ( "Emulateshiftheld" ). Invoke ( This );
} Catch
(Exception E1 ){

Keyevent shiftpressevent = New
Keyevent ( 0 ,
0 ,
Keyevent. action_down, keyevent. keycode_shift_left,
0 , 0 );
Shiftpressevent. Dispatch (mwebview );
}
} }

Add the oncreatecontextmenulistener listener to webview and add the above Code.

If you do not want to call the system's text selection function, you can also refer to the open source class library
Btandroidwebviewselection.

 

Related Article

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.