Get html element from my own web page from a WebView?

前端 未结 1 1595
暗喜
暗喜 2021-02-10 00:25

I have a webview displaying a page from my own server. Is it possible to add a javascript method that would allow my android app to read out a page element from the webview? Som

1条回答
  •  孤街浪徒
    2021-02-10 01:29

    Ok this is how i did it:

    
    
       
         
       
    
      
        
    data

    Then my activity:

    WebView webview = (WebView)findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.addJavascriptInterface(new JavascriptAccessor(), "javascriptAccessor");
    webview.setWebViewClient(new WebViewClient() {}); // wouldn't work without this!
    webview.loadUrl(url);
    
    private class JavascriptAccessor {
        @SuppressWarnings("unused")
        public void getYerData(String data) {
            Log.v(TAG, data);
        }
    }
    

    Yeah!

    0 讨论(0)
提交回复
热议问题