采用html编辑界面ui&java_javascript代码的互相调用

package cn.itcast.web;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class DemoActivity extends Activity {
	private WebView webView;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webView = (WebView) this.findViewById(R.id.webview);
        //相当于创建了一个浏览器 

        
        
        
        WebSettings settings = webView.getSettings(); // 得到浏览器的设置
        settings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new Object(){
        	public void callPhone(){
        		System.out.println("callphone");
        		Intent intent = new Intent();
        		intent.setAction(Intent.ACTION_CALL);
        		intent.setData(Uri.parse("tel:1351234567"));
        		startActivity(intent);
        	}
        	
        }, "demo");
        String  url = getResources().getString(R.string.serverurl);
        webView.loadUrl(url);
    }
	
	
	public void calljavascript(View view){
		webView.loadUrl("javascript:fillContent()");
	}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<Button
        android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="调用javascript"
    android:onClick="calljavascript"
    >
    
</Button>
<WebView 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/webview"
    />

</LinearLayout>


服务器端代码:

<html >   
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
    <script>  
        function fillContent(){   
            document.getElementById("content").innerHTML =    
                 "java调用javascript哈哈,这些话事javascript搞出来的";   
        }         
</script>     
<body>   
    <p><a onClick="window.demo.callPhone()" href="">打电话</a></p>   
    <p id="content"></p>   
    <p>java和javascript相互调用</p>   
</body>   
</html>



你可能感兴趣的:(采用html编辑界面ui&java_javascript代码的互相调用)