如何在android webview中获取选定的文本 [英] How to get the selected text in android webview

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

问题描述

我需要从 webview 中获取选定的文本,并且必须永久突出显示该文本.我尝试选择文本,当我尝试使用剪贴板获取文本时,它在clipboard.getText()"处显示空指针异常.我试过代码为

I need to get the selected text from webview and have to highlight the text permanently. I tried as selecting the text and while i am attempting to get the text using clipboard it showing me null pointer exception at "clipboard.getText()". I have tried code as

ClipboardManager mClipboard =
        (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

 KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                                                    KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(webview);
 if(mClipboard!=null)
     {
       String text = mClipboard.getText().toString();
       Toast.makeText(this, "select_text_now     "+text, Toast.LENGTH_LONG).show();
     }

我需要进一步突出显示所选文本..请帮助我..

I need to get the selected text for highlight futher..please help me..

推荐答案

我为此提交了一个 Android 错误:问题 24842:WebView 应该公开 getSelection.如果您查看故障单,您会看到一些不同的解决方法.

I have filed an Android bug for this: Issue 24842: WebView should make getSelection public. If you look at the ticket, you'll see some various workarounds.

请注意,您发布的代码与 emulateShiftHeld 大致相似,后者在 4.0 中已弃用,这可能是它在 2.2 而不是 4.0 中工作的原因.

Note that the code you posted is roughly similar to emulateShiftHeld which is deprecated in 4.0, which is probably why it is working in 2.2 and not in 4.0.

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

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