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

公司最近使用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,看来还有好多要学的