package com.ada56.apps.taxi.ui.login;

 

import android.annotation.SuppressLint;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.webkit.JsResult;

import android.webkit.WebChromeClient;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

import android.widget.Toast;

 

import com.ada56.apps.taxi.passenger.R;

 

public class JSAndroidActivity extends Activity {

    private WebView myWebView = null;

    @SuppressLint("SetJavaScriptEnabled")

    @Override

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_jsdemo);

        myWebView = (WebView) findViewById(R.id.myWebView);

        WebSettings webSettings = myWebView.getSettings();// 得到设置属性的对象

        webSettings.setJavaScriptEnabled(true);// 使能JavaScript

        webSettings.setDefaultTextEncodingName("GBK");//支持中文,否则页面中中文显示乱码

        // 限制在WebView中打开网页,而不用默认浏览器

        myWebView.setWebViewClient(new WebViewClient());

 

        // 如果不设置这个,JS代码中的按钮会显示,但是按下去却不弹出对话框

        // Sets the chrome handler. This is an implementation of WebChromeClient

        // for use in handling JavaScript dialogs, favicons, titles, and the

        // progress. This will replace the current handler.

        myWebView.setWebChromeClient(new WebChromeClient() {

            @Override

            public boolean onJsAlert(WebView view, String url, String message, JsResult result) {

                return super.onJsAlert(view, url, message, result);

            }

 

        });

 

        // 用JavaScript调用Android函数:

        // 先建立桥梁类,将要调用的Android代码写入桥梁类的public函数

        // 绑定桥梁类和WebView中运行的JavaScript代码

        // 将一个对象起一个别名传入,在JS代码中用这个别名代替这个对象,可以调用这个对象的一些方法

        //myWebView.addJavascriptInterface(new WebAppInterface(this), "myInterfaceName");

        myWebView.addJavascriptInterface(new WebViewNative(this), "Native");

 

        //myWebView.loadUrl("file:///android_asset/sample.html");// 载入页面:本地html资源文件

        myWebView.loadUrl("http://192.168.211.61:8080/taxi/jsAndroid.html");

    }

    

    public void callJSMethod(View view){

    //用Android代码调用JavaScript函数:

        myWebView.loadUrl("javascript:myFunction('<>')");

        //这里实现的效果和在网页中点击第一个按钮的效果一致

    }

 

    /**

     * 自定义的Android代码和JavaScript代码之间的桥梁类

     */

    public class WebViewNative {

        Context mContext;

        /** Instantiate the interface and set the context */

        WebViewNative(Context c) {

            mContext = c;

        }

        /** Show a toast from the web page */

        public void showToast(String toast) {

            Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show();

        }

    }

 

}

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/myRelativeLayout"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

 

   

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:padding="40dp"

        android:text="@string/hello_world"

        tools:context=".WebJSActivity" />

 

   

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_toRightOf="@id/textView1"

        android:onClick="callJSMethod"

        android:text="android_Button" />

 

   

        android:id="@+id/myWebView"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:layout_below="@id/textView1" />

 

 

===========================================================================================================================

    

   

       

        JavaScript函数调用

       

       
 

       

       

       

       

   

    

   

       

        用JavaScript按钮调用Android代码
 

       

            value="Say hello" onClick="showAndroidToast('Hello Android!')" />

   

 

    Google

   

 

更多相关文章

  1. Android(安卓)——单例封装SharedPreferences
  2. Android(安卓)NavigationBar 代码分析记录(一)
  3. 安卓面试中遇到的问题2017.2
  4. RecyclerView实现横向滚动效果
  5. android studio 打release包报错:Lint found fatal errors while
  6. Android(安卓)加载assets中的资源文件实例代码
  7. Android(安卓)NFC架构分析
  8. Android电源管理简介(下)
  9. android 笔记 --- Android中开发中常用代码

随机推荐

  1. Android群英传笔记——第六章:Android绘图
  2. Android中文翻译组 - 简介
  3. android UI进阶之android中隐藏的layout
  4. 在Android中实现Hook机制的实验
  5. 最封闭的开源系统,话说Android的八宗罪(转
  6. Android应用开发笔记(13): Android移动应
  7. 从0开始学Android之Android生命周期
  8. Android(安卓)自定义阴影Shadow颜色,大小
  9. Android(安卓)HAL实现的三种方式(1) - 基
  10. 对2016年android实习就业的一些看法