Android的:如何选择的WebView文本 [英] Android: how to select texts from webview
问题描述
我想允许用户选择的WebView一些文本,它需要被发送文本消息。请找到方法来选择文本复制到剪贴板,并从剪贴板中提取。我看到很多例子,但没有任何帮助我真的... TIA
编辑
使用从 @ orangmoney52提供的链接中的code 。有以下变化
GetMethod的的第二个参数调用方法的第二个参数。如果我给空也警告会..这是正确的?
公共无效selectAndCopyText(){
尝试 {
方法M = WebView.class.getMethod(emulateShiftHeld,Boolean.TYPE);
m.invoke(BookView.mWebView,假);
}赶上(例外五){
e.printStackTrace();
// 倒退
KeyEvent的转变pressEvent =新的KeyEvent(0,0,
KeyEvent.ACTION_DOWN,KeyEvent.KEY code_SHIFT_LEFT,0,0);
移pressEvent.dispatch(本);
}
}
收到此错误:
05-26 16:41:01.121:WARN / System.err的(1096):java.lang.NoSuchMethodException:emulateShiftHeld
以上答案看起来完全没有问题,它接缝你失去了一些东西,而选择text.So你需要仔细检查code和找到你overrided任何< STRONG>的TouchEvent 。
我试过code以下正常工作......
功能是
私人无效emulateShiftHeld(的WebView视图)
{
尝试
{
KeyEvent的转变pressEvent =新的KeyEvent(0,0,KeyEvent.ACTION_DOWN,
KeyEvent.KEY code_SHIFT_LEFT,0,0);
移pressEvent.dispatch(视图);
Toast.makeText(这一点,select_text_now,Toast.LENGTH_SHORT).show();
}
赶上(例外五)
{
Log.e(DD,异常的emulateShiftHeld(),E);
}
}
拨打上面的方法,无论你想(你可以把一个按钮,并调用这个方法在它的Click事件):emulateShiftHeld(mWebView);
i want allow user to select some texts from webview and it need to be send as a text message. pls find way to select text and copy to clipboard and extracting from clipboard. i saw many example but nothing helped me really...TIA
Edit
using the code provided in the link from @orangmoney52. with following changes
getmethod's second parameter and invoke method second parameter. if i give null there warning will come.. which one is correct?
public void selectAndCopyText() {
try {
Method m = WebView.class.getMethod("emulateShiftHeld", Boolean.TYPE);
m.invoke(BookView.mWebView, false);
} catch (Exception e) {
e.printStackTrace();
// fallback
KeyEvent shiftPressEvent = new KeyEvent(0,0,
KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
shiftPressEvent.dispatch(this);
}
}
Getting this error:
05-26 16:41:01.121: WARN/System.err(1096): java.lang.NoSuchMethodException: emulateShiftHeld
The above answers looks perfectly fine and it seams you missing something while selecting text.So you need to double check the code and find you overrided any TouchEvent of webview.
i Tried below code it works fine...
Function is
private void emulateShiftHeld(WebView view)
{
try
{
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(view);
Toast.makeText(this, "select_text_now", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Log.e("dd", "Exception in emulateShiftHeld()", e);
}
}
Call Above method wherever you want (You can put a button and call this method in its click event): emulateShiftHeld(mWebView);
这篇关于Android的:如何选择的WebView文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!