简单的混合开发

来源:互联网 发布:石家庄网络推广招聘 编辑:程序博客网 时间:2024/06/05 00:34


//布局

<?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="mvpframework.bwie.com.a1509ajs.MainActivity">


    <Button
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="调用js代码" />


    <WebView
        android:id="@+id/w"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>


//JS布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>调用Android组件测试</title>
<script type="text/javascript">
    function show(info){
        document.getElementById("shows").innerHTML = info;
    }
</script>
</head>

<body>
<b>测试</b>
<br />
<button onClick="window.hello.javaMethod('param')">启动hello world Activity</button>
<br />
<hr  color="#99FF00"/>
<button onClick="window.hello.showAndroid()">显示android内容</button>
<br />
<textarea id= "shows"  cols="20" rows="10"> 暂无记录 </textarea>
<br />

</body>
</html>


//MainActivity

package mvpframework.bwie.com.a1509ajs;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private WebView mW;
    String url = "file:///android_asset/a.html";
    /**
     * 调用js代码
     */
    private Button mBt;

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

    @SuppressLint("JavascriptInterface")
    private void initView() {
        mW = (WebView) findViewById(R.id.w);
        WebSettings settings = mW.getSettings();
        //设置是否支持js交互
        settings.setJavaScriptEnabled(true);
        //第二个参数也是js给的
        mW.addJavascriptInterface(this, "hello");

        //加载一个网页,这个网页加载的是assets目录里的a.html
        mW.loadUrl(url);



        mBt = (Button) findViewById(R.id.bt);
        mBt.setOnClickListener(this);
    }

    /**
     * 方法的名字是js给的
     */
    @JavascriptInterface
    public void showAndroid() {
        Log.e("MainActivity", "showAndroid");
        Toast.makeText(MainActivity.this, "js 调用Android 代码了", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt:
                mW.loadUrl("javascript:show('我是android传过来的代码')");
                break;
        }
    }
}


原创粉丝点击