webView 实现 与 javascript调用java方法(也称js调用native 方法) helloworld

来源:互联网 发布:货单打印软件 编辑:程序博客网 时间:2024/05/16 06:21

项目结构



效果图:




开发步骤:


1 创建普通的Java类:WebAppInterface.java


package com.exqw.adminx.myapplication;import android.content.Context;import android.webkit.JavascriptInterface;import android.widget.Toast;public class WebAppInterface {    Context mContext;    /** Instantiate the interface and set the context */    WebAppInterface(Context c) {        mContext = c;    }    /** Show a toast from the web page */    @JavascriptInterface    public void showToast(String toast) {        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();    }}

2 activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    app:layout_behavior="@string/appbar_scrolling_view_behavior"    tools:context="com.exqw.adminx.myapplication.MainActivity"    tools:showIn="@layout/activity_main">    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/webview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        /></RelativeLayout>

3 MainActivity.java文件

package com.exqw.adminx.myapplication;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.webkit.WebSettings;import android.webkit.WebView;import android.webkit.WebViewClient;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        WebView myWebView = (WebView) findViewById(R.id.webview);        myWebView.loadUrl("http://www.baidu.com");//        myWebView.loadUrl("file:///android_asset/hi.html");        myWebView.setWebViewClient(new MyWebViewClient());        WebSettings webSettings = myWebView.getSettings();        webSettings.setJavaScriptEnabled(true);        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");    }    private class MyWebViewClient extends WebViewClient {        @Override        public boolean shouldOverrideUrlLoading(WebView view, String url) {            if (Uri.parse(url).getHost().equals("www.example.com")) {                // This is my web site, so do not override; let my WebView load the page                return false;            }            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));            startActivity(intent);            return true;        }    }}


4 assets/index.html文件

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" /><script type="text/javascript">    function showAndroidToast(toast) {        Android.showToast(toast);    }</script>

0 0
原创粉丝点击