java代码:
package com...web;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.shushan.base.BaseActivity;
import com.shushan.base.ScreenManager;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com...R;
/**
* @ClassName: BaseWebActivity
* @Desciption: webview基础activity
* @author: jesse_android
* @date: 2018-05-10
*/
public class BaseWebActivity extends BaseActivity implements View.OnClickListener {
private static final String TAG = "BaseWebActivity";
protected WebView mWebView;
private ProgressBar progressBar;
private TextView titleTextView;
private ImageView backImg;
private TextView backTextView;
private TextView rightBackText;
protected String title;
protected String url;
private static final boolean enableProgressBar = false;
private boolean canBack = false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initWindow();
setContentView(R.layout.base_web_activity);
initView();
initWebView();
title = getIntent().getStringExtra("title");
if(!TextUtils.isEmpty(title)){
titleTextView.setText(title);
}
url = getIntent().getStringExtra("url");
Log.i(TAG,"url=" + url);
mWebView.loadUrl(url);
}
private void initWindow() {
ScreenManager screenManager;
screenManager = ScreenManager.getInstance();
screenManager.setStatusBar(true, this);
screenManager.setScreenRoate(true, this);
screenManager.setFullScreen(false, this);
}
private void initView() {
mWebView = findViewById(R.id.base_web_webview);
progressBar = findViewById(R.id.base_web_progress_bar);
titleTextView = findViewById(R.id.base_web_title_tv);
backImg = findViewById(R.id.base_web_left_back_img);
backImg.setOnClickListener(this);
backTextView = findViewById(R.id.base_web_title_back_tv);
backTextView.setOnClickListener(this);
rightBackText = findViewById(R.id.base_web_title_right_tv);
rightBackText.setOnClickListener(this);
if(canBack){
rightBackText.setVisibility(View.VISIBLE);
}else {
rightBackText.setVisibility(View.GONE);
}
}
private void initWebView() {
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setSupportZoom(true);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setDefaultTextEncodingName("utf-8");
mWebSettings.setLoadsImagesAutomatically(true);
//调用JS方法.安卓版本大于17,加上注解 @JavascriptInterface
mWebSettings.setJavaScriptEnabled(true);
mWebView.setWebChromeClient(webChromeClient);
mWebView.setWebViewClient(webViewClient);
}
WebViewClient webViewClient = new WebViewClient(){
/**
* 多页面在同一个WebView中打开,就是不新建activity或者调用系统浏览器打开
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
};
WebChromeClient webChromeClient = new WebChromeClient() {
@Override
public void onProgressChanged(WebView webView, int newProgress) {
super.onProgressChanged(webView, newProgress);
if(enableProgressBar){
if(newProgress == 100){
progressBar.setVisibility(View.GONE);//加载完网页进度条消失
} else{
progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
progressBar.setProgress(newProgress);//设置进度值
}
}
}
};
protected void onLeftBackward() {
Log.i(TAG,"onLeftBackward()");
onBackAction();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.base_web_left_back_img:
case R.id.base_web_title_back_tv:
goBack();
break;
case R.id.base_web_title_right_tv:
onLeftBackward();
break;
default:
break;
}
}
private void goBack() {
if(canBack){
if(mWebView.canGoBack()){
mWebView.goBack();
}else {
onLeftBackward();
}
}else {
onLeftBackward();
}
}
public boolean isCanBack() {
return canBack;
}
public void setCanBack(boolean canBack1){
canBack = canBack1;
}
}
这里导入的腾讯包如果找不到,换成android原生包即可。
如果需要拦截链接,在webViewClient中实现即可。
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@color/bg_navigation_bar"
android:orientation="vertical">
<View
android:id="@+id/base_web_divider"
android:layout_width="match_parent"
android:layout_height="20dp"
android:background="@color/bg_navigation_bar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/base_web_left_back_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:padding="0dp"
android:src="@drawable/ic_back_white" />
<TextView
android:id="@+id/base_web_title_back_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/base_web_left_back_img"
android:text="返回"
android:textColor="@color/white_text_color"
android:textSize="15sp" />
<TextView
android:id="@+id/base_web_title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text=""
android:textColor="@color/white_text_color"
android:textSize="18sp" />
<TextView
android:id="@+id/base_web_title_right_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:padding="5dp"
android:text="关闭"
android:textColor="@color/white_text_color"
android:textSize="15sp"
android:visibility="gone"/>
</RelativeLayout>
</LinearLayout>
<ProgressBar
android:id="@+id/base_web_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="3dip"
android:progressDrawable="@drawable/web_progress"
android:visibility="gone" />
<com.tencent.smtt.sdk.WebView
android:id="@+id/base_web_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
欢迎参观博主其他博客。