溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android開發之WebView與js的數據傳遞

發布時間:2020-07-18 05:20:23 來源:網絡 閱讀:987 作者:墨宇hz 欄目:移動開發

    在Android開發中不可避免的會出現,使用一個WebView來顯示一個h6頁面。如果僅僅只是顯示h6頁面,這不需要特殊的處理,但是很多情況下會出現,js和WebView的交互。比如:將html表單中的數據,傳遞到Activity中,在Activity中對數據校驗,再上傳到服務器。

    那么js和WebView如何進行的交互的呢?

    創建AndroidHtml項目。主要代碼如下:

MainActivity.java:

    private WebView mWebView;
    public static final String URL_TEST="file:///android_asset/index.html";

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView = (WebView) findViewById(R.id.webView);
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);

        mWebView.loadUrl(URL_TEST);
        //addJavascriptInterface這個方法中有兩個參數,第一個參數是添加一個對象,這個對象中封裝了在js中要調用的native方法,第二個參數是告訴js對象的名稱以便于調用native方法
        mWebView.addJavascriptInterface(this,"zzh");//
    }

    @JavascriptInterface//android 4.2之上的版本需要加上這個注解,表示將這個方法暴露給js調用
    public void callAndroidNativeMethod(String str){
        Log.d("MainActivity","---方法被執行了?。?!-");
        Toast.makeText(this, "---"+str, Toast.LENGTH_LONG).show();
    }

注解@JavascriptInterface注解在Android版本4.2之上必須要加上。Google官方的解釋是

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available to your JavaScript (the method must also be public). If you do not provide the annotation, the method is not accessible by your web page when running on Android 4.2 or higher.

index.html:

<!DOCTYPE html>
<HTML lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <script type="text/javascript" language="javascript">
            function callAndroidNativeMethod(){
                window.zzh.callAndroidNativeMethod("js中的數據傳遞到了Android中了");
            }
        </script>
    </head>
    <body>
        <button onclick="callAndroidNativeMethod()">點擊調用Java代碼</button>
    </body>
</HTML>

activity_main.xml中的布局是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.zzh.androidhtml.MainActivity">
    <Button
        android:id="@+id/button"
        android:text="Java調用js方法"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <WebView
        android:id="@+id/webView"
        android:layout_below="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>
</RelativeLayout>

好了,上面就是js中如何調用Android Native方法。下面來說說Android Native中怎么調用js方法(要注意Native中調用js方法來處理Android中的業務邏輯的效率差,不建議使用此方式處理Android業務邏輯,但是也要知道如何使用)。

private WebView mWebView;
public static final String URL_TEST="file:///android_asset/index.html";

@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebView = (WebView) findViewById(R.id.webView);
    WebSettings settings = mWebView.getSettings();
    settings.setJavaScriptEnabled(true);

    mWebView.loadUrl(URL_TEST);
    mWebView.addJavascriptInterface(this,"zzh");

    findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mWebView.loadUrl("javascript:callJavaScriptMethod()");
        }
    });
}

@JavascriptInterface//android 4.2之上的版本需要加上這個注解,表示將這個方法暴露給js調用
public void callAndroidNativeMethod(String str){
    Log.d("MainActivity","---方法被執行了?。?!-");
    Toast.makeText(this, "---"+str, Toast.LENGTH_LONG).show();
}

index.html文件比較簡單

<!DOCTYPE html>
<HTML lang="zh-CN">
<head>
    <meta charset="utf-8">
    <script type="text/javascript" language="javascript">
            function callAndroidNativeMethod(){
                window.zzh.callAndroidNativeMethod("js中的數據傳遞到了Android中了");
            }
            function callJavaScriptMethod(){
                document.getElementById("content").innerHTML += "<br\>java調用了js函數";
            }
    </script>
</head>
<body>
<button onclick="callAndroidNativeMethod()">點擊調用Java代碼</button>
<div id="content"></div>
</body>
</HTML>

 示例比較簡單,注釋也寫的比較清楚不做過多的講解,全憑自己領悟。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女