最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Android webview: highlight a specific word in a page using javascript? - Stack Overflow

programmeradmin2浏览0评论

i am using a webview inside my android application. I would like to know if it is possible to highlight or underline a specific word/sentence/paragraph in a loaded page using code done in javascript?

thanks

i am using a webview inside my android application. I would like to know if it is possible to highlight or underline a specific word/sentence/paragraph in a loaded page using code done in javascript?

thanks

Share Improve this question asked Mar 14, 2011 at 3:57 ccotccot 1,8733 gold badges37 silver badges54 bronze badges 1
  • yes, it's possible... what have you tried? – Cristian Commented Mar 14, 2011 at 4:01
Add a comment  | 

4 Answers 4

Reset to default 15

Why can't you try this? titleWebView is the WebView that has the html loaded or to be loaded. searchText should contain the text to be searched. I constructed a dialog to get the search terms from the user and reloaded the webpage each time the user hits the search. All the terms containing the searchText is highlighted (light green on my Nexus One). Hope this helps.

titleWebView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {
        if (searchText != null && !searchText.equals("")) {
            int i = titleWebView.findAll(searchText);
            Toast.makeText(getApplicationContext(), "Found " + i + " results !",
                Toast.LENGTH_SHORT).show();
            try {
                Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
                m.invoke(titleWebView, true);
            } catch (Throwable ignored) {
            }
            searchText = "";
        }
    }
});

You should look at this page. Do a view source.

They use pure Javascript--not even jQuery--to highlight particular words on the webpage.

Basically they load all of the text they want to search through into a variable and continuously find the indexOf the search term they want to highlight. When they find the index of the term, they build a new string with everything before that index + a <span class="highlighted"> + the search term + </span>. Then they continue searching. Once they've searched and rebuilt all the text they replace the old text in the DOM with the new text.

They also have some CSS along the lines of .highlighted { background-color: yellow; }

findAll() is deprecated! You can use findAllAsync() from API16 onwards in a simple manner like this:

webView.setWebViewClient(new WebViewClient(){
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onPageFinished(WebView view, String url) {
                if (searchText != null && !searchText.equals("")) {
                    webView.findAllAsync(searchText);
                }
            }
        });

Code

Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(titleWebView, true);

After this test using following code.

findAll("search text")

When text is match Toast display.

发布评论

评论列表(0)

  1. 暂无评论