WebView,androidwebview

來源:互聯網
上載者:User

WebView,androidwebview

前人經驗:

開原始檔控制:一個自訂的帶數字progress類:NumberProgressBar;

 

 

WebViewClient就是協助WebView處理各種通知、請求事件的。

WebChromeClient是輔助WebView處理Javascript的對話方塊,網站表徵圖,網站title,載入進度等 。

總結:別把簡單的東西複雜化。

NumberProgressBar.java

 

我的網盤:http://share.weiyun.com/cfda31dae65609c7376d0f9153678a27

 

style

 

<style name="NumberProgressBar_Funny_Orange">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>

<item name="progress_max">100</item>
<item name="progress_current">0</item>

<item name="progress_unreached_color">@android:color/transparent</item>
<item name="progress_reached_color">#f00722</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#f00722</item>

<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>

 

 

xml布局

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/ticket_web"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"></WebView>
<com.etoury.project.ui.view.NumberProgressBar
android:id="@+id/progressbar"
style="@style/NumberProgressBar_Funny_Orange"
android:layout_width="match_parent"
app:progress_reached_bar_height="2dp"
app:progress_text_size="0sp"
app:progress_text_visibility="invisible"
app:progress_unreached_bar_height="2dp" />

</FrameLayout>

java代碼

pr = (NumberProgressBar) findViewById(R.id.progressbar);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setBlockNetworkImage(false);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBlockNetworkLoads(false);
webView.getSettings().setGeolocationEnabled(true);
String url = "http://u.ctrip.com/union/CtripRedirect.aspx?TypeID=650&city="
+"北京"+"&cityid=1&sourceid=1&sid=777206&allianceid=303851&ouid=";
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webView.setVisibility(View.VISIBLE);
super.onPageFinished(view, url);
}

@Override
public void onLoadResource(WebView view, String url) {
if (url.contains("https://accounts.ctrip.com/H5Login/") || url.contains("http://m.ctrip.com/you/place/1")
|| url.contains("http://m.ctrip.com/webapp/diyshx/list/") || url.contains("http://m.ctrip.com/webapp/Hotel/hotel1")) {
view.stopLoading();
}
super.onLoadResource(view, url);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
super.onGeolocationPermissionsShowPrompt(origin, callback);
callback.invoke(origin, true, false);


}

@Override
public void onProgressChanged(WebView view, int newProgress) {
if (pr == null) return;
pr.setProgress(newProgress);
if (newProgress == 100) {
pr.setVisibility(View.GONE);
} else {
pr.setVisibility(View.VISIBLE);
}
super.onProgressChanged(view, newProgress);
}
});

 

聯繫我們

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