使用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,一开始的时候,网站那边要求我们 ...
随机推荐
- Problem A+B
Problem A+B Time Limit : 1000 MS Memory Limit : 65536 KB Description Calculate a+b Input Two ...
- FZU2224 An exciting GCD problem 区间gcd预处理+树状数组
分析:(别人写的) 对于所有(l, r)区间,固定右区间,所有(li, r)一共最多只会有log个不同的gcd值, 可以nlogn预处理出所有不同的gcd区间,这样区间是nlogn个,然后对于询问离线 ...
- HDU 3342
#include<stdio.h> #include<string.h> int degree[101],vis[101],map[101][101]; int main() ...
- win7 64位 VS2010调试提示“ORA-12154: TNS: 无法解析指定的连接标识符”的解决方法
这个问题刚刚遇到,花了半小时,记录下 环境: vs2010[32位] oracle 10g[32位] 操作系统:windows 7 64位 解决步骤: 1.去网上下载“instantclient- ...
- 【HTML】Beginner6:Link
1.Link HTML wich basically means a system of linked text link to another HTML file or any file a ...
- linux 小技巧总结
(1)linux判断文件是否存在 if [ -f filename]: then ......#要执行的语句 fi 具体例子: file=/usr/local/oracle/oradata ...
- Windows Azure 的磁盘管理相关概念
在 Windows Azure 的虚拟机中,磁盘有多种使用方式.操作系统磁盘是用来为虚拟机提供操作系统的虚拟硬盘.数据磁盘是附加到虚拟机上用来存储应用程序数据的 VHD. 根据应用程序的需要,可从多种 ...
- CodeForces 352D. Jeff and Furik
题意:给n个数,第一个人选取相邻两个递降的数交换顺序,第二个人一半的概率选取相邻两个递降的数交换顺序,一半的概率选取相邻两个递增的数交换顺序.两个人轮流操作,求整个数列变成递增数列所需交换次数的期望. ...
- hdoj 1898 Sempr == The Best Problem Solver?
Sempr == The Best Problem Solver? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/3276 ...
- [三]ajax重要属性
readState:0初始化,1建立,2已接收,3处理中,4请求完成,响应就绪 status:200成功,404未找到页面失败 onreadystatechange:状态改变自动调用的方法 respo ...