android中webview控件和javascript交互實(shí)例
當(dāng)我們要實(shí)現(xiàn)豐富的圖文混排效果的時候,我們一般會使用webview,這是一個功能十分強(qiáng)大的的控件,來看看官方的解釋:
一個能顯示網(wǎng)頁內(nèi)容的View。該類是你實(shí)現(xiàn)一個自己的瀏覽器,或者只是在activity中顯示網(wǎng)頁內(nèi)容的基礎(chǔ);它基于WebKit內(nèi)核來顯示網(wǎng)頁,并且包含了實(shí)現(xiàn)前后翻頁、放大縮小,文字搜索方法。
從上面你應(yīng)該了解到了基本功能,也就是顯示網(wǎng)頁。這篇文章中我們主要討論webview和Javascript的交互。如果你的js基礎(chǔ)比java基礎(chǔ)好的話那么采用這種方式做一些復(fù)雜的處理是個不錯的選擇。
WebView和js的交互包含兩方面,一是在html中通過js調(diào)用安卓的java代碼;二是在安卓java代碼中調(diào)用js。
一、html中通過js調(diào)用java代碼
js中調(diào)用java代碼其實(shí)就記住一點(diǎn),webview設(shè)置一個和js交互的接口(注意這里只是一般的意思,并不是java中接口的含義),這個接口其實(shí)是一個一般的類,同時為這個接口取一個別名。這個過程如下:
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(),"demo");
new DemoJavaScriptInterface就是這個接口,demo就是這個接口的別名。
上面的代碼執(zhí)行之后在html的js中就能通過別名(這里是“demo”)來調(diào)用newDemoJavaScriptInterface類中的任何方法。
如我們想讓html中的一個button點(diǎn)擊之后調(diào)用java中的函數(shù)可以這樣:
<input type="button" value="click me" onclick="window.demo.clickOnAndroid()"/>
但是因?yàn)榘踩珕栴},在Android4.2中(如果應(yīng)用的android:targetSdkVersion數(shù)值為17+)JS只能訪問帶有 @JavascriptInterface注解的Java函數(shù)。因此如果你的開發(fā)版本比較高,需要在被調(diào)用的函數(shù)前加上@JavascriptInterface注解。
下面是谷歌給出的代碼示例:
WebViewDemo.java
package com.google.android.webviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
/**
* Demonstrates how to embed a WebView in your activity. Also demonstrates how
* to have javascript in the WebView call into the activity, and how the activity
* can invoke javascript.
* <p>
* In this example, clicking on the android in the WebView will result in a call into
* the activities code in {@link DemoJavaScriptInterface#clickOnAndroid()}. This code
* will turn around and invoke javascript using the {@link WebView#loadUrl(String)}
* method.
* <p>
* Obviously all of this could have been accomplished without calling into the activity
* and then back into javascript, but this code is intended to show how to set up the
* code paths for this sort of communication.
*
*/
public class WebViewDemo extends Activity {
private static final String LOG_TAG = "WebViewDemo";
private WebView mWebView;
private Handler mHandler = new Handler();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
mWebView.loadUrl("file:///android_asset/demo.html");
}
final class DemoJavaScriptInterface {
DemoJavaScriptInterface() {
}
/**
* This is not called on the UI thread. Post a runnable to invoke
* loadUrl on the UI thread.
*/
public void clickOnAndroid() {
mHandler.post(new Runnable() {
public void run() {
mWebView.loadUrl("javascript:wave()");
}
});
}
}
/**
* Provides a hook for calling "alert" from javascript. Useful for
* debugging your javascript.
*/
final class MyWebChromeClient extends WebChromeClient {
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
Log.d(LOG_TAG, message);
result.confirm();
return true;
}
}
}
demo.html
<html>
<script language="javascript">
/* This function is invoked by the activity */
function wave() {
alert("1");
document.getElementById("droid").src="android_waving.png";
alert("2");
}
</script>
<body>
<!-- Calls into the javascript interface for the activity -->
<a onClick="window.demo.clickOnAndroid()"><div style="width:80px;
margin:0px auto;
padding:10px;
text-align:center;
border:2px solid #202020;" >
<img id="droid" src="android_normal.png"/><br>
Click me!
</div></a>
</body>
</html>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/intro"
android:padding="4dip"
android:textSize="16sp"
/>
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
二、android調(diào)用js
上面的代碼在演示如何在js中調(diào)用java代碼的同時也演示了如何在java中調(diào)用js
調(diào)用形式:
其中wave()是js中的一個方法,當(dāng)然你可以把這個方法改成其他的方法,也就是android調(diào)用其他的方法。
demo的解釋:
現(xiàn)在你一定了解了android和js的交互了。是時候分析一些demo了,根據(jù)上面講的你也應(yīng)該比較清楚了。具體交互流程如下:
①點(diǎn)擊圖片,則在js端直接調(diào)用android上的方法clickOnAndroid();
②clickOnAndroid()方法(利用線程)調(diào)用js的方法。
③被②調(diào)用的js直接控制html。
個人總結(jié):利用webView的這種方式在有些時候UI布局就可以轉(zhuǎn)成相應(yīng)的html代碼編寫了,而html布局樣式之類有DW這樣強(qiáng)大的工具,而且網(wǎng)上很多源碼,很多代碼片。在UI和視覺效果上就會節(jié)省很多時間,重復(fù)發(fā)明輪子沒有任何意義。
- android WebView加載html5介紹
- Android中實(shí)現(xiàn)Webview頂部帶進(jìn)度條的方法
- Android WebView使用方法詳解 附j(luò)s交互調(diào)用方法
- android webview中使用Java調(diào)用JavaScript方法并獲取返回值
- 在Android系統(tǒng)中使用WebViewClient處理跳轉(zhuǎn)URL的方法
- Android中Webview打開網(wǎng)頁的同時發(fā)送HTTP頭信息方法
- Android開發(fā)之WebView組件的使用解析
- 解析Android中webview和js之間的交互
- Android中 webView調(diào)用JS出錯的解決辦法
- Android Webview滑進(jìn)出屏幕閃爍的解決方法
相關(guān)文章
Android下拉刷新SwipeRefreshLayout控件使用方法
這篇文章主要介紹了Android下拉刷新SwipeRefreshLayout控件使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android開發(fā)之全屏與非全屏的切換設(shè)置方法小結(jié)
這篇文章主要介紹了Android開發(fā)之全屏與非全屏的切換設(shè)置方法,結(jié)合實(shí)例形式分析了Android全屏切換靜態(tài)與動態(tài)兩種實(shí)現(xiàn)方法,需要的朋友可以參考下2017-08-08android實(shí)現(xiàn)圖片反轉(zhuǎn)效果
這篇文章主要介紹了android實(shí)現(xiàn)圖片反轉(zhuǎn)效果的方法,需要的朋友可以參考下2015-09-09Android sqlite cursor的遍歷實(shí)例詳解
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于Android sqlite cursor的遍歷的相關(guān)實(shí)例及知識點(diǎn),需要的朋友們可以學(xué)習(xí)下。2021-06-06android實(shí)現(xiàn)掃描網(wǎng)頁二維碼進(jìn)行網(wǎng)頁登錄功能
這篇文章主要介紹了android掃描網(wǎng)頁二維碼進(jìn)行網(wǎng)頁登錄效果,掃描成功之后跳轉(zhuǎn)進(jìn)入主頁,具體實(shí)現(xiàn)代碼大家參考下本文2017-12-12Android 實(shí)現(xiàn)當(dāng)下最流行的吸頂效果
本文主要介紹了Android 實(shí)現(xiàn)當(dāng)下最流行的吸頂效果的示例代碼。具有很好的參考價值,下面跟著小編一起來看下吧2017-03-03