Android - Highlight text in WebView above android

2019-01-20 04:12发布

I have a webview that performs a search, this code works great for below android version 4.0.3, at this version and higher the highlight function fails to highlight. The search itself works and jumps the page around but nothing is shown for the result (Unless you look hard enough!).

I want to know if anyone knows of a work-around that supports higher than 4.0.3?

People in other answers have linked to these webpages as possible solutions but I am not sure how to adapt this to my webview in Android.

WebPages:

http://4umi.com/web/javascript/hilite.php#thescript
http://www.nsftools.com/misc/SearchAndHighlight.htm

Code I am using currently to search and highlight:

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && ((keyCode == KeyEvent.KEYCODE_ENTER))) {
                wv.findAll(findBox.getText().toString());

                try {
                    Method m = WebView.class.getMethod("setFindIsUp",
                            Boolean.TYPE);
                    m.invoke(wv, true);
                } catch (Exception ignored) {
                }
            }
            return false;
        }

1条回答
三岁会撩人
2楼-- · 2019-01-20 05:05

In Android 4.1(jellybean), WebView.findAll() is deprecated, we should use WebView.findAllAsync instead.

reference

Wish this help:)

查看更多
登录 后发表回答