使用WebView显示网页
简单的页面跳转

package com.example.webtest;
import java.security.PublicKey;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private String url = "http://2014.qq.com/";
private ProgressDialog progressDialog;
private WebView web;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
web = (WebView) findViewById(R.id.web);
// Uri uri = Uri.parse(url);
// URL:统一资源定位符 也就是网址 例如 http://www.microsoft.com/
// URI:通用资源标志符 http://www.acme.com/support/suppliers.htm
// Intent intent = new Intent(Intent.ACTION_VIEW, uri);
// startActivity(intent);
init();
/*
* Uri uri = Uri.parse(url); // url为你要的链接 Intent intent = new
* Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
*/
}
private void init() {
// TODO Auto-generated method stub
// Log.d("tag", "DAYIN");
web.loadUrl(url);
// web.loadUrl("file:///android_asset/1.html");
web.setWebViewClient(new WebViewClient() {
// 覆盖webView默认通过第三方或者是系统浏览器打开网页的行为,使得网页可以在WebView中打开
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
// 返回值是TRUE的时候控制网页在webview中打开,如果为false的时候则调用系统浏览器或者第三方浏览器打开
return super.shouldOverrideUrlLoading(view, url);
}
});
// Webviewclient帮助webview去处理一些页面控制和请求通知。
WebSettings settings = web.getSettings();
settings.setJavaScriptEnabled(true);// 使得手机自动适应网页
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // webvie加载页面优先使用缓存加载
web.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
super.onProgressChanged(view, newProgress);
if (newProgress == 100) {
// 网页加载完毕, 关闭progressDialog
closeDialog();
} else {
openDialog(newProgress);
}
}
private void openDialog(int newProgress) {
// TODO Auto-generated method stub
if (progressDialog == null) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("正在加载中");
progressDialog
.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
progressDialog.setProgress(newProgress);
progressDialog.show();
} else {
progressDialog.setProgress(newProgress);// 显示新的进度
}
}
private void closeDialog() {
// TODO Auto-generated method stub
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();// 取消显示
progressDialog = null;
}
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 改写物理按键--返回的逻辑
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (web.canGoBack()) {
web.goBack();
return true;// 在javaScript里,return有终止函数的执行和传递数值,两种功能。
// return false 就是返回假值,从而终止了函数的执行
// return true 就是返回真值,从而传递数值,继续执行函数
} else {
System.exit(0);
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
web.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" >
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>
使用WebView显示网页的更多相关文章
- Android 之 用WebView显示网页
WebView可以使得网页轻松的内嵌到 app 里,还可以直接跟js相互调用. WebView有两个方法:setWebChromeClient 和 setWebClient (1) setWebCli ...
- Android 使用WebView显示网页
构建WebView就可以显示Web信息.因为我觉得这里会讲述很多方式来实现WebView,所以我决定为每一种方式创建一个对应的Activity,MainActivity通过Button可以点击进入对应 ...
- HTML5学习总结-11 IOS 控件WebView显示网页
一 加载外部网页 1.使用UIWebView加载网页 运行XCode 新建一个Single View Application . 2 添加安全消息 添加以下消息到项目的 Info.plist &l ...
- HTML5学习总结-10 Android 控件WebView显示网页
WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient 1)setWebClient: ...
- Android:控件WebView显示网页
WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient setWebClient:主要 ...
- Android:控件WebView显示网页 -摘自网络
WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient setWebClient:主要 ...
- WebView 显示网页
1.布局 <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:androi ...
- android WebView控件显示网页
有时需要app里面显示网页,而不调用其他浏览器浏览网页,那么这时就需要WebView控件.这个控件也是很强大的,放大,缩小,前进,后退网页都可以. 1.部分方法 //支持javascriptweb.g ...
- 安卓Webview缓存网页数据(无网络正常显示)
热度 1已有 52 次阅读2016-8-26 17:53 |个人分类:常见问题|系统分类:移动开发 一.需求经历 最近的项目是一个原生 +webview 显示的 APP,一开始的时候,网站那边要求我们 ...
随机推荐
- windows平台下VLC2.0.5编译
windows平台下VLC2.0.5编译说明 时隔一年多,又要搞流媒体了,不过这次是要做流媒体服务器. 暂时决定使用vlc+ffmpeg+live555,虽然听有些前辈说这个组合的性能较差,只能作为学 ...
- [Hadoop源码解读](三)MapReduce篇之Job类
下面,我们只涉及MapReduce 1,而不涉及YARN. 当我们在写MapReduce程序的时候,通常,在main函数里,我们会像下面这样做.建立一个Job对象,设置它的JobName,然后配置输入 ...
- Running an etcd cluster on localhost
Purpose Run a cluster on localhost while investigating etcd Use a static cluster (So we have no exte ...
- Log4net 写文件日志与数据库日志
一.数据库日志表结构 CREATE TABLE [dbo].[WebLog_Msg]( [LogID] [int] IDENTITY(1,1) NOT NULL, [Date] [datetime] ...
- Visual Studio 2015 下载地址
Visual Studio 2015 发行说明: https://visualstudio.com/zh-cn/news/vs2015-vs.aspx Visual Studio 2015 特性简 ...
- jquery 列求和
列求和 var m = 0; $('#tb tr').each(function () { //td:eq(3)从0开始计数 $(this).find('td:eq(3)').each(functio ...
- Datatable转换成List实体对象列表 几个实例
一, /// <summary> /// 将Datatable转换为List集合 /// </summary> /// <typeparam name="T&q ...
- Phonegap3.4 环境搭建及新建项目
一.环境准备 1.到这里安装Node.js. 2.到这里下载Adroid ADT Bundle for Windows,下载后解压,我的放在:F:\MobileDev\adt-bundle-windo ...
- 《Introduction to Algorithm》-chaper33-计算几何学
叉积: 在平面中我们为了度量一条直线的倾斜状态,为引入倾斜角这个概念.而通过在直角坐标系中建立tan α = k,我们实现了将几何关系和代数关系的衔接,这其实也是用计算机解决几何问题的一个核心,计算机 ...
- weekend110(Hadoop)的 第一天笔记
(2015年1月10日) 课程目录 01-hadoop职位需求状况 02-hadoop课程安排 03-hadoop应用场景 04-hadoop对海量数据处理的解决思路 05-hadoop版本选择和伪分 ...