Android:如何从 webview 中选择文本 [英] Android: how to select texts from webview

查看:40
本文介绍了Android:如何从 webview 中选择文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望允许用户从 webview 中选择一些文本,并且需要将其作为短信发送.请找到选择文本并复制到剪贴板并从剪贴板中提取的方法.我看到了很多例子,但没有什么能真正帮助我...TIA

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

编辑
使用来自 @orangmoney52 的链接中提供的代码.有以下变化

Edit
using the code provided in the link from @orangmoney52. with following changes

getmethod 的第二个参数和 invoke 方法的第二个参数.如果我给 null 会出现警告..哪个是正确的?

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);
        }

}

出现此错误:

 05-26 16:41:01.121: WARN/System.err(1096): java.lang.NoSuchMethodException: emulateShiftHeld  

推荐答案

上面的答案看起来非常好,而且您在选择文本时似乎遗漏了一些东西.因此,您需要仔细检查代码并找到您覆盖的 webview 的任何 TouchEvent.

The above answers looks perfectly fine and it seems you're missing something while selecting text. So you need to double check the code and find your overridden any TouchEvent of webview.

我试过下面的代码它工作正常...

i Tried below code it works fine...

功能是

 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);
        }
    }

在任何你想要的地方调用上面的方法(你可以放置一个按钮并在它的点击事件中调用这个方法):emulateShiftHeld(mWebView);

Call Above method wherever you want (You can put a button and call this method in its click event): emulateShiftHeld(mWebView);

这篇关于Android:如何从 webview 中选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆