開發安卓-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,看來還有好多要學的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章