• 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内容改变

      

      

  • 相关阅读:
    poj3277 City Horizon
    60.左值右值以及类型判断
    59.C++与正则表达式
    57.C++处理转义字符
    56.lambda表达式与绑定以及伪函数和绑定
    55.函数模板指针匹配(模板自动匹配*多的)
    54.函数模板默认参数
    53.伪函数与函数绑定器
    52.模板的重载
    51.模板与引用
  • 原文地址:https://www.cnblogs.com/ganchuanpu/p/5990366.html
Copyright © 2020-2023  润新知