效果图
方式一:
核心代码
package com.ican.subjects.activity;
import android.app.ProgressDialog;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.ican.subjects.R;
import com.ican.subjects.base.BaseActivity;
/**
* 创建者 涂文远
* 创建时间 2016/8/31 16:10
* 描述 ${TODO}
*
* 更新者 $Author$
* 更新时间 $Date$
* 更新描述 ${TODO}
*/
public class WebViewActivity extends BaseActivity {
private WebView mWebView;
private ProgressDialog mProgressDialog;
@Override
public int getLayout() {
return R.layout.activity_webview;
}
@Override
protected void initView() {
mWebView = (WebView) findViewById(R.id.blog_detail_webview);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setIcon(R.mipmap.ic_launcher);
mProgressDialog.setTitle( "进度对话框" );
mProgressDialog.setMessage( "玩命下载中...." );
// 设置对话框的样式为水平
mProgressDialog.setProgressStyle(ProgressDialog. STYLE_HORIZONTAL);
mProgressDialog.setMax(100);
mProgressDialog.show();
mWebView.setWebViewClient(new WebViewClient(){
/*@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl("http://www.2000df.com");
return true;
}*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl("http://www.2000df.com");
return true;
}
});
}
@Override
protected void initData() {
mWebView.setWebChromeClient(new MyWebChromeClient());
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setUseWideViewPort(true);//设置此属性,可任意比例缩放
settings.setLoadWithOverviewMode(true);
settings.setBuiltInZoomControls(true);
settings.setSupportZoom(true);
// User settings
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (0 != (getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE)) {
WebView.setWebContentsDebuggingEnabled(true);
}
}
settings.setJavaScriptEnabled(true); //设置webview支持javascript
settings.setLoadsImagesAutomatically(true); //支持自动加载图片
settings.setUseWideViewPort(true); //设置webview推荐使用的窗口,使html界面自适应屏幕
settings.setLoadWithOverviewMode(true);
settings.setSaveFormData(true); //设置webview保存表单数据
settings.setSavePassword(true); //设置webview保存密码
settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM); //设置中等像素密度,medium=160dpi
settings.setSupportZoom(true); //支持缩放
CookieManager.getInstance().setAcceptCookie(true);
if (Build.VERSION.SDK_INT > 8) {
settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
}
// Technical settings
settings.setSupportMultipleWindows(true);
mWebView.setLongClickable(true);
mWebView.setScrollbarFadingEnabled(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mWebView.setDrawingCacheEnabled(true);
settings.setAppCacheEnabled(true);
settings.setDatabaseEnabled(true);
settings.setDomStorageEnabled(true);*/
mWebView.loadUrl("http://www.2000df.com");
}
private class MyWebChromeClient extends WebChromeClient {
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
//Toast.makeText(WebViewActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
//progress.setVisibility(View.GONE);
} else {
/*if (progress.getVisibility() == View.GONE)
progress.setVisibility(View.VISIBLE);
progress.setProgress(newProgress);*/
mProgressDialog.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
方式二:
核心代码
package com.xuecheyi.activity;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import com.xuecheyi.BaseActivity;
import com.xuecheyi.mb.R;
import com.xuecheyi.utils.Constant;
import com.xuecheyi.utils.DialogUtil;
import com.xuecheyi.utils.LogUtil;
import com.xuecheyi.utils.SPUtils;
import com.xuecheyi.views.TitleManager;
import com.xuecheyi.views.XCYJavascriptInterface;
import org.json.JSONObject;
@SuppressLint("SetJavaScriptEnabled")
public class LoadZTWebViewActivity extends BaseActivity implements
OnClickListener {
private WebView mWebview;
private WebSettings settings;
private String titleStr, urlPath, path, adPath;
private String mType;
private Dialog loadDialog;
private LinearLayout title_back_ll;
private XCYJavascriptInterface mXCYJavascriptInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zt_protocol);
findViews();
init();
}
@Override
protected void onRestart() {
super.onRestart();
if(Constant.IsFromLogin) {
findViews();
init();
Constant.IsFromLogin = false;
}
}
public void initCS() {
if (Build.VERSION.SDK_INT >= 19) {
mWebview.getSettings().setLoadsImagesAutomatically(true);
} else {
mWebview.getSettings().setLoadsImagesAutomatically(false);
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.indexOf("tel:") < 0) {// 页面上有数字会导致连接电话
view.loadUrl(url);
}
return true;
}
}
@Override
public void findViews() {
title_back_ll = (LinearLayout) findViewById(R.id.title_back_ll);
// TODO Auto-generated method stub
mWebview = (WebView) findViewById(R.id.mWebview);
settings = mWebview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
mXCYJavascriptInterface = new XCYJavascriptInterface(mWebview,this);
mWebview.addJavascriptInterface(mXCYJavascriptInterface,
"XCYAndroid");
// 设置webview缓存
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
//initCS();
}
@Override
public void init() {
// TODO Auto-generated method stub
loadDialog = DialogUtil.getLoadingDialog(this);
titleStr = getIntent().getStringExtra("titleStr");
mType = getIntent().getStringExtra("type");
path = getIntent().getStringExtra("path");
adPath = getIntent().getStringExtra("adPath");
if (mType != null && mType.equals("DriverSchool")) {
String jxId = getIntent().getStringExtra("jxId");
urlPath = Constant.BASE_WEBVIEW_PATH + "jx_detail.html?jxId="
+ jxId;
mWebview.loadUrl(urlPath);
} else if (mType != null && mType.equals("DriverTeacher")) {
String jlId = getIntent().getStringExtra("jlId");
urlPath = Constant.BASE_WEBVIEW_PATH + "jl_detail.html?jlId="
+ jlId;
mWebview.loadUrl(urlPath);
} else if (mType != null && mType.equals("FAQ")) {
settings.setCacheMode(WebSettings.LOAD_DEFAULT);
urlPath = Constant.BASE_WEBVIEW_PATH + "FAQ.html";
mWebview.loadUrl(urlPath);
}
TitleManager.showTitleByBack(this, null, titleStr, R.string.title_back,
0, this);
if (null != path) {
urlPath = "file:///android_asset/html/" + path;
mWebview.loadUrl(urlPath);
}
if (null != adPath) {
urlPath = adPath+"&token="+getToken();
//urlPath = adPath;
mWebview.loadUrl(urlPath);
}
LogUtil.e("####", "##显示网址##" + urlPath);
mWebview.setWebViewClient(new MyWebViewClient());
mWebview.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
loadDialog.show();
}
@Override
public void onPageFinished(WebView view, String url) {
loadDialog.dismiss();
}
});
mWebview.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
return super.onJsAlert(view, url, message, result);
}
});
}
public String getToken() {
String token = (String) SPUtils.get(this, Constant.UserInfo.USER_TOKEN, "");
LogUtil.e("####", "token:" + token);
if (token != null && !token.equals("")) {
return token;
} else {
return "";
}
}
@Override
public void setListener() {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.title_back_ll:
if (path != null && path.equals("splash")) {
Intent intent = new Intent(LoadZTWebViewActivity.this,
MainActivity.class);
startActivity(intent);
LoadZTWebViewActivity.this.finish();
} else {
LoadZTWebViewActivity.this.finish();
}
break;
}
}
@Override
public void onRecvData(String type, JSONObject response) {
// TODO Auto-generated method stub
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (path != null && path.equals("splash")) {
Intent intent = new Intent(LoadZTWebViewActivity.this,
MainActivity.class);
startActivity(intent);
LoadZTWebViewActivity.this.finish();
} else {
if (mWebview.canGoBack()) {
mWebview.goBack();
return true;
} else {
finish();
}
}
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
protected void onPause() {
super.onPause();
mWebview.onPause();
}
@Override
protected void onResume() {
super.onResume();
mWebview.onResume();
}
}