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.