混合開發-webview和原生交互

混合開發在移動開發中很常見,比如qq中的運動,釐米秀等功能都是用網頁實現的。
混合開發中一個重要的功能就是網頁和原生接口的數據交互,下面將實現一個小demo.

新建一個activity,佈局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.h.learn.WebViewActivity">

    <WebView
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:id="@+id/my_web_view"
        />

    <LinearLayout
        android:background="@drawable/webview_bg"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        >
        <EditText
            android:id="@+id/edit_info"
            android:text="text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btn_confirm"
            android:text="確定"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/log_text_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>

佈局中定義了一個webview,在屏幕上半部分。
底部是原生的一個輸入框和按鈕,輸入框用於輸入數據,按鈕用於調用網頁中的接口。
網頁中也應該是類似的,一個輸入框和一個調用原生接口的按鈕。
assets文件夾中的網頁代碼如下:

<!DOCTYPE html>
<html>
<head>
    <title></title>

</head>
<body>
    <script>
        //供原生代碼調用
        function showEditInfo(str){
            alert(str);
        };
        //調用原生接口,並把網頁中文本框的內容傳過去
        function callNative()
        {
            var str=document.getElementById("mytext").value;
            window.webview.actionFromJsWithParam(str);
        };
        //alert(1);
        //showEditInfo('123');
    </script>
    <input type="text" value="123" name="mytext" id="mytext">
    <button  onClick="callNative()">調用原生接口</button>


</body>
</html>

public class WebViewActivity extends AppCompatActivity {

    @BindView(R.id.my_web_view)WebView webview;
    @BindView(R.id.edit_info)EditText editInfo;
    @BindView(R.id.log_text_view)TextView logTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        ButterKnife.bind(this);
        //webview = (WebView) findViewById(R.id.my_web_view);
        Log.d("flag--","onCreate(WebViewActivity.java:20)-->>"+webview);

        initView();
    }

    //調用js接口,並把editText中的內容傳到網頁接口中
    @OnClick(R.id.btn_confirm)
    public void confirm(){
        Log.d("flag--","confirm(WebViewActivity.java:35)-->>"+"javascript:showEditInfo('"+editInfo.getText()+"')");
        webview.loadUrl("javascript:showEditInfo('"+editInfo.getText()+"')");
    }

    private void initView() {
        //加載網頁
        webview.loadUrl("file:///android_asset/webview.html");

        //允許彈框
        webview.setWebChromeClient(new WebChromeClient());

        WebSettings webSettings= webview.getSettings();
        //允許運行js
        webSettings.setJavaScriptEnabled(true);
        webview.addJavascriptInterface(this, "webview");
    }

    //供js調用的接口
    @android.webkit.JavascriptInterface
    public void actionFromJsWithParam(final String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String text = logTextView.getText() +  "\njs調用了Native函數傳遞參數:" + str;
                logTextView.setText(text);
            }
        });

    }

}

webview.addJavascriptInterface(this, “webview”);中第二個參數可以自定義,但要和js中相對應,因此js中調用原生接口的方法爲 window.webview.actionFromJsWithParam(str);

最終效果:

這裏寫圖片描述

這裏寫圖片描述

常用框架:

https://weex.apache.org/cn/guide/

http://www.ionic.wang/js_doc-index.html

http://www.wex5.com/wex5/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章