前言:最近重构毕业设计,经过实习后,觉得以前写的真的不算好,很多东西还可以优化,重构,代码复用。现在写的也不算漂亮,但是对自己整个大学生涯要有个总结。
添加加载进度条,WebView封装成工具类,传输数据对象
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.WebViewActivity">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="2dp"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/progressbar"
android:max="100"
android:visibility="visible"/>
<WebView
android:id="@+id/webView"
android:layout_below="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<ImageButton
android:id="@+id/arrow_left"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_weight="1"
android:background="@color/color_ffffff"
app:srcCompat="@mipmap/arrow_left"/>
<ImageButton
android:id="@+id/arrow_right"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_weight="1"
android:background="@color/color_ffffff"
app:srcCompat="@mipmap/arrow_right"/>
</LinearLayout>
</RelativeLayout>
public class WebViewUtil {
@SuppressLint("JavascriptInterface")
public void ClientWebView(final Activity mactive, WebView webView, final ProgressBar progressBar, String url){
WebSettings webSettings = webView.getSettings();
//支持js
webSettings.setJavaScriptEnabled(true);
// 是否可访问本地文件,默认值 true
webSettings.setAllowFileAccess(true);
//自适应屏幕
webSettings.setLoadWithOverviewMode(true);
//动态更新内容
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
//支持自动加载图片
webSettings.setLoadsImagesAutomatically(true);
// 是否支持缩放
webSettings.setSupportZoom(false);
// 设置出现缩放工具
webSettings.setBuiltInZoomControls(true);
//扩大比例的缩放
webSettings.setUseWideViewPort(true);
//自适应屏幕
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webSettings.setPluginState(WebSettings.PluginState.ON);
// 是否可用Javascript(window.open)打开窗口,默认值 false
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
// 设置可现实js的alert弹窗
webView.setWebChromeClient(new WebChromeClient());
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
progressBar.setProgress(newProgress);
if (newProgress == 100) {
progressBar.setVisibility(View.GONE);
}
}});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
WebResourceResponse response = null;
response = super.shouldInterceptRequest(webView, webResourceRequest);
return response;
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView webView, String s) {
WebResourceResponse response = null;
response = super.shouldInterceptRequest(webView, s);
String[] namelist = s.split("/");
return response;
}
});
webView.loadUrl(url);
}
}
传递一个对象,该对象使用serializable序列化
public class WebUrl implements Serializable {
String title;
String url;
public WebUrl(String title, String url) {
this.title = title;
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
跳转到WebViewActivity,携带对象
skipWebView("两学一做", "http://www.bhu.edu.cn/newsgl/page/lxyz/");
private void skipWebView(String title, String url) {
WebUrl webUrl = new WebUrl(title, url);
Bundle bundle = new Bundle();
bundle.putSerializable("WebUrl", webUrl);
startActivity(WebViewActivity.class, bundle);
}
WebViewActivity.class
public class WebViewActivity extends BaseActivity {
@BindView(R.id.progressBar)
ProgressBar progressBar;
@BindView(R.id.webView)
WebView webView;
@Override
protected int contentViewID() {
return R.layout.activity_webview;
}
@Override
protected void initialize() {
Intent intent = getIntent();
WebUrl webUrl = (WebUrl) intent.getSerializableExtra("WebUrl");
setTopTitle(webUrl.getTitle(), true);
setLeftBtnFinish();
WebViewUtil webViewUtil = new WebViewUtil();
webViewUtil.ClientWebView(this,webView,progressBar,webUrl.getUrl());
}
@OnClick({R.id.arrow_left, R.id.arrow_right})
public void onClick(View view) {
switch (view.getId()){
case R.id.arrow_left:
if (webView.canGoBack()) {
webView.goBack();
} else {
Toast.makeText(this, getString(R.string.tip_web_left), Toast.LENGTH_LONG).show();
return;
}
break;
case R.id.arrow_right:
if (webView.canGoForward()) {
webView.goForward();
} else {
Toast.makeText(this, getString(R.string.tip_web_right), Toast.LENGTH_LONG).show();
return;
}
break;
default:
}
}
}