Android开发之带进度条的WebView
效果:
直接上代码:
区分java和kotlin版本
Java版本:
package com.example.progresswebview;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
/**
* @author xiayiye5
* 2020年6月5日14:47:18
*/
public class JavaWebViewActivity extends AppCompatActivity {
private ProgressBar pbAd;
private WebView wvInternetPage;
private TextView tvWebTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN |
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED);
setContentView(R.layout.activity_webview_browser);
String loadUrl = getIntent().getStringExtra("load_url");
loadUrl = "https://blog.csdn.net/xiayiye5";
String title = getIntent().getStringExtra("title");
title = "下一页5博客首页";
wvInternetPage = findViewById(R.id.pb_webView);
tvWebTitle = findViewById(R.id.tv_web_title);
pbAd = findViewById(R.id.pb_ad);
WebViewOption(loadUrl, title);
}
private void WebViewOption(String loadUrl, String title) {
tvWebTitle.setText(title);
WebSettings settings = wvInternetPage.getSettings();
settings.setJavaScriptEnabled(true);
//允许打开js新窗口
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setDomStorageEnabled(true);
wvInternetPage.setWebViewClient(new MyWebViewClient());
wvInternetPage.setWebChromeClient(new MyChromeWebViewClient());
wvInternetPage.loadUrl(loadUrl);
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
tvWebTitle.setText(view.getTitle());
super.onPageFinished(view, url);
}
}
private class MyChromeWebViewClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
pbAd.setVisibility(View.INVISIBLE);
} else {
if (pbAd.getVisibility() == GONE) {
pbAd.setVisibility(VISIBLE);
}
pbAd.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
}
@Override
public void onBackPressed() {
if (wvInternetPage.canGoBack()) {
wvInternetPage.goBack();
return;
}
super.onBackPressed();
}
}
kotlin版本:
package com.example.progresswebview
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
/**
* @author xiayiye5
* 2020年6月5日14:47:18
*/
class KotlinWebViewActivity : AppCompatActivity() {
private var pbAd: ProgressBar? = null
private var wvInternetPage: WebView? = null
private var tvWebTitle: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN or
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED
)
setContentView(R.layout.activity_webview_browser)
var loadUrl = intent.getStringExtra("load_url")
loadUrl = "https://blog.csdn.net/xiayiye5/category_7513606.html"
var title = intent.getStringExtra("title")
title = "下一页5|git的使用"
wvInternetPage = findViewById(R.id.pb_webView)
tvWebTitle = findViewById(R.id.tv_web_title)
pbAd = findViewById(R.id.pb_ad)
WebViewOption(loadUrl, title)
}
private fun WebViewOption(loadUrl: String, title: String) {
tvWebTitle!!.text = title
val settings = wvInternetPage!!.settings
settings.javaScriptEnabled = true
//允许打开js新窗口
settings.javaScriptCanOpenWindowsAutomatically = true
settings.domStorageEnabled = true
wvInternetPage!!.webViewClient = MyWebViewClient()
wvInternetPage!!.webChromeClient = MyChromeWebViewClient()
wvInternetPage!!.loadUrl(loadUrl)
}
private inner class MyWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(
view: WebView,
url: String
): Boolean {
return super.shouldOverrideUrlLoading(view, url)
}
override fun onPageFinished(view: WebView, url: String) {
tvWebTitle!!.text = view.title
super.onPageFinished(view, url)
}
}
private inner class MyChromeWebViewClient : WebChromeClient() {
override fun onProgressChanged(view: WebView, newProgress: Int) {
if (newProgress == 100) {
pbAd!!.visibility = View.INVISIBLE
} else {
if (pbAd!!.visibility == View.GONE) {
pbAd!!.visibility = View.VISIBLE
}
pbAd!!.progress = newProgress
}
super.onProgressChanged(view, newProgress)
}
}
override fun onBackPressed() {
if (wvInternetPage!!.canGoBack()) {
wvInternetPage!!.goBack()
return
}
super.onBackPressed()
}
}
再看下布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:orientation="vertical">
<TextView
android:id="@+id/tv_web_title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:gravity="center"
android:textColor="#000000"
tools:text="标题" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#999999" />
<ProgressBar
android:id="@+id/pb_ad"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="2dp"
android:progress="34"
android:progressDrawable="@drawable/custom_progress" />
<WebView
android:id="@+id/pb_webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
进度条drawable资源:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<gradient
android:endColor="#EDEDED"
android:startColor="#EDEDED" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip><!--可裁剪对象-->
<shape android:shape="rectangle">
<corners android:radius="5dp" />
<gradient
android:angle="45"
android:endColor="#FB9162"
android:startColor="#F84B61" />
</shape>
</clip>
</item>
</layer-list>