android webview用法小結2 java與javascript的互動

來源:互聯網
上載者:User

先提下兩種方式

 方式一:

//先載入html檔案,然後調用html檔案中的javascript函數 

webView.loadUrl("file:///android_asset/show.html"); 

webView.loadUrl("javascript:pushNewsData('"+strHtml+"')");    

   方式二:

//在Java代碼中添加javascript的介面,然後在html檔案中調用java中的對象  

webView.addJavascriptInterface(strHtml, "MyContent");

先讓瀏覽器支援javascript

webView.getSettings().setJavaScriptEnabled(true);//開啟就可以用js的功能

Js代碼,放在asset檔案夾下,也可以在伺服器端,這樣改變內容,只需改伺服器端,用戶端不用修改

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><script type="text/javascript">function btn_test() {/* 列印,logcat裡的tag是Web Console */console.log("btn_test");}function call_java() {/* js調用java代碼,需要介面 */Window.interface_test.showToastFromWeb("hello i am javascript!");}function test_alert() {/* 彈出對話方塊 */alert("hello i am javascript!");}function test_confirm() {/* 彈出帶確定和取消按鈕的對話方塊 */var v = confirm("hello i am javascript!");console.log("onfirm " + v);}function test_prompt() {/* 彈出帶輸入框的對話方塊 */var v = prompt("input you name!", "tom");console.log("prompt " + v);}</script></head><body><button onclick="btn_test()">test</button><button onclick="call_java()">call_java</button><button onclick="test_alert()">test_alert</button><button onclick="test_confirm()">test_confirm</button><button onclick="test_prompt()">test_prompt</button><label tel:800800284>tel:8008233</label></body></html>

Java代碼裡

對應call_java()方法

webView.addJavascriptInterface(new Object(){public void showToastFromWeb(String msg){showToast(msg);}}, "interface_test");

對應alert等方法

webView.setWebChromeClient(new WebChromeClient(){@Overridepublic void onProgressChanged(WebView view, int newProgress) {//這裡上一博文已提過}@Overridepublic boolean onJsAlert(WebView view, String url, String message,JsResult result) {// TODO Auto-generated method stubreturn super.onJsAlert(view, url, message, result);}@Overridepublic boolean onJsConfirm(WebView view, String url,String message, JsResult result) {// TODO Auto-generated method stubreturn super.onJsConfirm(view, url, message, result);}@Overridepublic boolean onJsPrompt(WebView view, String url, String message,String defaultValue, JsPromptResult result) {// TODO Auto-generated method stubreturn super.onJsPrompt(view, url, message, defaultValue, result);}});

載入html

//webView.loadUrl("javascript:btn_test()");webView.loadUrl("file:///android_asset/test.html");


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.