WebView

1 篇文章 / 0 new
author
WebView
載入 asset 目錄下檔案
方法一:使用 loadUrl
webview = (WebView)findViewById(R.id.webview);
webview.loadUrl("file:///android_asset/h01.html");

方法二:使用 loadData,注意此方式內部的相關連結均會無效
String data = "";
try {  
    InputStream is = getAssets().open("h01.html");  
    ByteArrayBuffer baf = new ByteArrayBuffer(500);  
    int count = 0;  
    while ((count = is.read()) != -1) {  
        baf.append(count);  
    }  
    data = EncodingUtils.getString(baf.toByteArray(), "utf-8");  
} catch (IOException e) {  
    e.printStackTrace();  
}  
// 下兩種方式均可  
webView.loadData(data, "text/html", "utf-8");  
//webView.loadDataWithBaseURL("", data, "text/html", "utf-8", "");

載入遠端 url
WebView webView = (WebView) findViewById(R.id.calculator);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportZoom(true);//支援縮放
webView.getSettings().setDefaultZoom(ZoomDensity.FAR);//顯示之解析度
webView.getSettings().setBuiltInZoomControls(true);//出現縮放工具
 
webView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int progress) {
        activity.setTitle("Loading...");
        activity.setProgress(progress * 100);
        if (progress == 100)
            activity.setTitle(R.string.app_name);
    }
});
webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        // Handle the error
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});
webView.loadUrl("http://www.google.com");
直接載入文字
final String htmlText = "<html><head></head><body>" +
                   "<table><tr><td>" +
                   "<A href=\"http://shioulo.no-ip.org\"><img src=\"pic.jpg" /></A></td></tr>" +     
                   "</table></body></html>";
webView.loadDataWithBaseURL("", htmlText, "text/html", "utf-8", null);
Free Web Hosting