Android 实现带进度条的WebView的实例

Android 实现带进度条的WebView的实例

1. WebView加载网页方法

//加载本地资源 loadUrl("file:///android_asset/example.html"); //加载网络资源 loadUrl("http://baidu.com");

2. 带进度的Drawable文件view_progress_webview

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/progress"> <clip> <shape> <solid android:color="#31CE15"/> <corners android:radius="2dp"/> </shape> </clip> </item> </layer-list>

颜色值为进度颜色,根据需要更换

3. ProgressWebView类

/** * 带进度条的WebView * @Author GQ */ public class ProgressWebView extends WebView { private ProgressBar progressbar; private Context mContext; public ProgressWebView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal); progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 5, 0, 0)); Drawable drawable = context.getResources().getDrawable(R.drawable.view_progress_webview); progressbar.setProgressDrawable(drawable); addView(progressbar); //主要处理解析,渲染网页等浏览器做的事情 setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); //加载失败时候,显示自定义的页面 if (errorListener != null) { errorListener.onError(); } } }); //辅助WebView处理Javascript的对话框,网站图标,网站title,加载进度等 setWebChromeClient(new WebChromeClient()); getSettings().setSupportZoom(true);//是否可以缩放 getSettings().setBuiltInZoomControls(true); getSettings().setJavaScriptEnabled(true);//支持JS getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); getSettings().setUseWideViewPort(true); getSettings().setLoadWithOverviewMode(true); getSettings().setSaveFormData(true); getSettings().setDomStorageEnabled(true); //优先使用缓存 getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //禁用长按 setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View view) { return true; } }); //如果在浏览器下载,调用浏览器默认下载+通知栏 setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); mContext.startActivity(intent); } }); } public class WebChromeClient extends android.webkit.WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress == 100) { progressbar.setVisibility(GONE); } else { if (progressbar.getVisibility() == GONE) progressbar.setVisibility(VISIBLE); progressbar.setProgress(newProgress); } super.onProgressChanged(view, newProgress); } @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); if (titleListener != null) titleListener.getTitle(title); } } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { LayoutParams lp = (LayoutParams) progressbar.getLayoutParams(); lp.x = l; lp.y = t; progressbar.setLayoutParams(lp); super.onScrollChanged(l, t, oldl, oldt); } private TitleListener titleListener; public interface TitleListener { void getTitle(String title); } public void setOnTitleListener(TitleListener titleListener) { this.titleListener = titleListener; } private ErrorListener errorListener; public interface ErrorListener { void onError(); } public void setOnErrorListener(ErrorListener errorListener) { this.errorListener = errorListener; } }

4. 使用

/** * 公共WebView */ public class BasicWebActivity extends Activity { protected ProgressWebView progressWebView; private TextView title;//标题栏 private TextView tv_none;//加载失败显示文字 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.common_webview); title = (TextView) findViewById(R.id.title); tv_none = (TextView) findViewById(R.id.tv_none); progressWebView = (ProgressWebView) findViewById(R.id.progressWebView); String url = getIntent().getStringExtra("url"); progressWebView.setOnTitleListener(new ProgressWebView.TitleListener() { @Override public void getTitle(String title) { title.setText(title); } }); progressWebView.setOnErrorListener(new ProgressWebView.ErrorListener() { @Override public void onError() { tv_none.setText("url资源失效"); } }); //加载网页 progressWebView.loadUrl(url); } //重写返回键 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (progressWebView.canGoBack()) { progressWebView.goBack();//返回上一层页面 return true; } else { finish();//关闭页面 } } return super.onKeyDown(keyCode, event); } }

其中common_webview就包含一个title一个progressWebView就不贴代码了。

如有疑问,请留言或者到本站社区交流讨论,本站关于Android开发的文章还有很多,还希望大家搜索参阅,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

时间: 2024-08-31 11:09:21

Android 实现带进度条的WebView的实例的相关文章

Android实现带进度条的WebView

如果不使用系统自带的TitleBar(即Activity被设置@android:style/Theme.NoTitleBar),那就需要自己来写进度条了,这里封装了一个自定义控件和加载网页的公共Activity,方便使用. 一.截图 二.自定义控件 /** * 带进度条的WebView * @author 农民伯伯 * @see http://www.cnblogs.com/over140/archive/2013/03/07/2947721.html * */ @SuppressWarning

【Android】带进度条的WebView

前言  如果不使用系统自带的TitleBar(即Activity被设置@android:style/Theme.NoTitleBar),那就需要自己来写进度条了,这里封装了一个自定义控件和加载网页的公共Activity,方便使用.   声明  欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com      正文 一.截图     二.自定义控件   /**  * 带进度条的WebView  *

【Android】自定义带进度条的WebView,修复不弹出软键盘的BUG

记录下最近开发中研究的带进度条的WebView 自定义类吧. 其实网上有不少这样的帖子,但是都没有一个完整的好用的例子,最关键的是,用网上的例子后有一个很明显的bug,就是遇到输入框的话没法弹出软键盘.研究了好久总算搞定了.特此记录下. 直接上源代码,关于代码的解释,个人感觉注释中已经添加的足够清楚了. ProgressWebView import android.content.Context; import android.content.res.Resources; import andr

带进度条的webview

     如果不使用系统自带的TitleBar(即Activity被设置@android:style/Theme.NoTitleBar),那就需要自己来写进度条了,这里封装了一个自定义控件和加载网页的公共Activity,方便使用.   声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com      正文 一.截图     二.自定义控件   复制代码 /**  * 带进度条的WebVie

自定义带进度条的WebView , 增加获取web标题和url 回掉

1.自定义ProgressWebView package com.app.android05; import android.content.Context; import android.graphics.Bitmap; import android.util.AttributeSet; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; /

PHP+Ajax异步带进度条上传文件实例_php实例

最近项目中要做一个带进度条的上传文件的功能,学习了Ajax,使用起来比较方便,将几个方法实现就行. 前端引入文件 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min

PHP+Ajax异步带进度条上传文件实例

最近项目中要做一个带进度条的上传文件的功能,学习了Ajax,使用起来比较方便,将几个方法实现就行. 前端引入文件 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min

android实现带进度条的文件上传

MainActivity.java package com.example.upload; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Pro

Android中实现Webview顶部带进度条的方法_Android

写这篇文章,做份备忘,简单滴展示一个带进度条的Webview示例,进度条位于Webview上面. 示例图如下: 主Activity代码: 复制代码 代码如下: package com.droidyue.demo.webviewprogressbar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.vi