android使用web載入網頁的js問題

來源:互聯網
上載者:User

標籤:android   webview   

android好久沒有用了,用它來打包個html5遊戲,代碼如下


protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);          WebView webView=new WebView(this);        webView.loadUrl("http://www.girl4493.cn/);//多加上這句話就可以了        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.girl4493.cn");        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);    }}

與君共勉



本文出自 “yang的技術專欄” 部落格,請務必保留此出處http://13438626.blog.51cto.com/13428626/1980441

android使用web載入網頁的js問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.