Android WebView Java与Js通信


1.Activity 中 事件修改 webview中内容

2.webview中事件修改activity中的内容

3.webview中的事件,响应js函数


主要代码实现: 

@SuppressLint("JavascriptInterface") public class MainActivity extends Activity {

    private static final String TAG="LocalBrowser";
    private final Handler handler=new Handler();
    private WebView webView;
    private TextView textView;
    private Button button;
    
    private class AndroidBridge //这个类中提供各种js可调用的方法。
    {
        public void callAndroid(final String arg)
        {
            handler.post(new Runnable(){
                public void run()
                {
                    Log.d(TAG,"calAndroid("+arg+")");
                    textView.setText(arg);
                }
            });
        }
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        webView=(WebView)findViewById(R.id.web_view);
        textView=(TextView)findViewById(R.id.text_view);
        button=(Button)findViewById(R.id.button);
        
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new AndroidBridge(), "android"); //将那个实例化的函数类设置为”android"的js接口。
        //主要处理解析,渲染网页等浏览器做的事情
        webView.setWebChromeClient(new WebChromeClient()
        {
            //(WebView上alert是弹不出来东西的,需要定制你的WebChromeClient处理弹出) 
            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                    JsResult result) {
                
                Log.d(TAG,"onJsAlert("+view+","+url+","+message+","+result+")");
                Toast.makeText(MainActivity.this, message, 3000).show();
                result.confirm();
                return true;
            }
            
        });//设置可以被java截获的js事件。

        //通过这里切换 assets 中的 html文件 
        webView.loadUrl("file:///android_asset/index.html");
        button.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                Log.d(TAG,"onClick("+v+")");
                webView.loadUrl("javascript:callJS('hello from android')");  //java调用js的函数
            }
        });
    }
    
}


js html1文件   

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>hello world</title>
		<script type="text/javascript">
			function helloworld() {
				alert("hello world!")
			}
			
		</script>
	</head>

	<body>
		This is a show “helloworld” default page,your can click this button
		<button οnclick="helloworld()">click me!</button> to show a "hello world" message!
	</body>

</html>


项目源码地址:http://download.csdn.net/detail/flyingsir_zw/9490172



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值