• Java调用JavaScript


    1.main.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        >  
        <TextView    
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
            android:text="Welcome to Mr Wei's Blog."  
            />  
        <WebView  
            android:id="@+id/webview"  
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
        />  
        <Button  
            android:id="@+id/button"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:text="Change the webview content"  
        />  
    </LinearLayout> 
    

    2.demo.html

    <html>  
        <mce:script language="javascript"><!--  
       
            function fillContent(){  
                document.getElementById("content").innerHTML =   
                     "This Content is showed by Android invoke Javascript function.";  
            }  
          
    // --></mce:script>    
      <body>  
        <p><a onClick="window.demo.startMap()" href="">Start GoogleMap</a></p>  
        <p id="content"></p>  
        <p>A Demo ----Android and Javascript invoke each other.</p>  
        <p>Author:Frankiewei</p>  
      </body>  
    </html>
    

    3.WebViewDemo.java

    package com.tutor.webwiewdemo;
    import android.app.Activity;
    import android.content.ComponentName;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.widget.Button;
    public class WebViewDemo extends Activity {
    	private WebView mWebView;
    	private Button mButton;
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		setupViews();
    	}
    	//初始化
    	private void setupViews() {
    		mWebView = (WebView) findViewById(R.id.webview);
    		WebSettings mWebSettings = mWebView.getSettings();
    		//加上这句话才能使用javascript方法
    		mWebSettings.setJavaScriptEnabled(true);
    		//增加接口方法,让html页面调用
    		mWebView.addJavascriptInterface(new Object() {
    			//这里我定义了一个打开地图应用的方法
    			public void startMap() {
    				Intent mIntent = new Intent();
    				ComponentName component = new ComponentName(
    						"com.google.android.apps.maps",
    						"com.google.android.maps.MapsActivity");
    				mIntent.setComponent(component);
    				startActivity(mIntent);
    			}
    		}, "demo");
    		//加载页面
    		mWebView.loadUrl("file:///android_asset/demo.html");
    		mButton = (Button) findViewById(R.id.button);
    		//给button添加事件响应,执行JavaScript的fillContent()方法
    		mButton.setOnClickListener(new Button.OnClickListener() {
    			public void onClick(View v) {
    				mWebView.loadUrl("javascript:fillContent()");
    			}
    		});
    	}
    }
    

      

               

                  首界面                                               点击按钮时,html内容改变

      

      

  • 相关阅读:
    【转】Django部署时为什么要用 uWSGI与 Nginx? 以及 WSGI,uwsgi等协议
    django 的 uwsgi+Nginx 部署配置
    【转】详解Django DRF框架中APIView、GenericAPIView、ViewSet区别
    python导入包 相对路径踩坑
    【转】Jmeter逻辑控制器-事务控制器的使用
    my live / PC keyboard / Thinkpad Mluti Connect Bluttooth Keyboard with Trackpoint / KT-1525 / KU-1255 / 4x30k12182
    my live / PC GPU NVIDIA Quadro P1000 / Intel UHD Graphics 630 / Dell P2418HT / chumoping
    project architecture evolution
    OS + Linux DevOps
    OS + Linux MP3
  • 原文地址:https://www.cnblogs.com/ganchuanpu/p/5990366.html
Copyright © 2020-2023  润新知