开发安卓-android使用webview加载网页无法使用js的问题

发布时间:2020-06-01 17:46:11 作者:jiyue0520_624
来源:网络 阅读:4484

公司最近使用android打包html5的app应用,直接解决了浏览器打开的问题

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         WebView webView=new WebView(this);
        webView.loadUrl("http://bmwcar.61tg.com/);	//多加上这句话就可以了
        webView.setWebViewClient(new MyWebViewClient());

     }

使用时发现所有的js都无法使用来,找来半天终于知道问题在哪里了,使用webview默认是吧js关闭的,因此是不会执行js代码的,这个时候只需要加上一句话就够了



webView.getSettings().setJavaScriptEnabled(true);//支持js

是的,这句话就够了,true表示支持js false表示不支持js,默认是不支持的,图样啊


完整代码如下:


package activity.ysmall.cc.ysmall;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.webkit.WebView;public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
         WebView webView=new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);//支持js        webView.loadUrl("http://www.ysmall.cc/mobile");
        webView.setWebViewClient(new MyWebViewClient());


    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;
    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;
        }        return super.onOptionsItemSelected(item);
    }
}

bingo,看来还有好多要学的

推荐阅读:
  1. WebView加载网页(二)
  2. WebView加载网页(一)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

浏览器 js代码 package

上一篇:php中变量的申明方法

下一篇:MySQL如何计算两个坐标的距离

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》