码迷,mamicode.com
首页 > 移动开发 > 详细

webview中js调用Android中的方法

时间:2014-07-19 00:39:34      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   java   color   

package com.example.helloworld;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.webkit.WebView;
 
public class MainActivity extends Activity {
    private WebView webView = null; 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        webView = (WebView)this.findViewById(R.id.webView);  
        //设置字符集编码  
        webView.getSettings().setDefaultTextEncodingName("UTF-8");  
        //开启JavaScript支持  
        webView.getSettings().setJavaScriptEnabled(true);  
        webView.addJavascriptInterface(this, "myObject");  
        //加载网页文件  
        String url = "http://*****/test.html";  
        webView.loadUrl(url);  
    }
     
    public void changeActivity() {
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, ForwardTarget.class);
        startActivity(intent);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}
<input type="button" value="跳转Activity" onclick="window.myObject.changeActivity();" />

其中:myObject是在JAVA中定义的名字,changeActivity是JAVA中定义的方法。

webview中js调用Android中的方法,布布扣,bubuko.com

webview中js调用Android中的方法

标签:android   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/andy2018/p/3853870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!