Android中在WebView中使用javascript

來源:互聯網
上載者:User

 

預設情況下,在WebView中是不能使用javascript的。可以通過書寫下面的代碼:

 

WebView myWebView = (WebView) findViewById(R.id.webview);

WebSettings webSettings = myWebView.getSettings();

webSettings.setJavaScriptEnabled(true);

 

然後就可以使用了。

 

綁定javascript和Android代碼:

 

例如:你可以在你的應用程式中建立一個類:

 

public class JavaScriptInterface {

    Context mContext;

 

    /** Instantiate the interface and set the context */

    JavaScriptInterface(Context c) {

        mContext = c;

    }

 

    /** Show a toast from the web page */

    public void showToast(String toast) {

        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();

    }

}

 

綁定javascript,定義的介面名稱為Android。

 

WebView webView = (WebView) findViewById(R.id.webview);

webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

 

然後在你的HTML代碼中寫:

 

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

 

<script type="text/javascript">

    function showAndroidToast(toast) {

        Android.showToast(toast);

    }

</script>

 

注意:綁定的javascript運行在另一個線程中,與建立它的線程不是同一個。

 

處理頁面導航:

 

預設情況下,當你單擊你的WebView頁面的連結時,串連到URIs。你可以使用下面的方式串連到你自己的WebView。

 

WebView myWebView = (WebView) findViewById(R.id.webview);

myWebView.serWebViewClient(new WebViewClient());

 

如何你想控制更多的點擊串連的話,可以重寫shouldOverrideUrlLoading()方法,建立自己的WebViewClient:

 

private class MyWebViewClient extends WebViewClient {

    @Override

    public boolean shouldOverrideLoading(WebView view, String url) {

        if (Uri.parse(url).getHost().equals("www.example.com")) {

            // This is my web site, so do not override; let my WebView load the page

            return false;

        }

        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        startActivity(intent);

        return true;

    }

}

 

通過下面的方式來訪問曆史頁:

 

public boolean onKeyDown(int keyCode, KeyEvent event) {

    // Check if the key event was the BACK key and if there's history

    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack() {

        myWebView.goBack();

        return true;

    }

    // If it wasn't the BACK key or there's no web page history, bubble up to the default

    // system behavior (probably exit the activity)

    return super.onKeyDown(keyCode, event);

}

摘自:燕龍安的專欄

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.