webview是Android里面加载网页的一个控件,下面通过一个实例来说明加载网页的方法以及webview的使用方法。
1.直接通过浏览器来加载一个网页
1.1获取网络应用权限
<uses-permission android:name="android.permission.INTERNET"/>
1.2加载网页
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
2.通过webview来打开一个网页
1.1获取网络应用权限
<uses-permission android:name="android.permission.INTERNET"/>
1.2注册webview控件
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webview"/>
WebView web = (WebView)findViewById(R.id.webview);
1.3加载网页(此方式为同过外部的浏览器来打开网页)
private String url = "http://www.34NF.com";
web.loadUrl(url);
1.4在webview上面来打开网页
web.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
if(newProgress == 100) {
closeDialog();
} else {
openDialog(newProgress);
}
super.onProgressChanged(view, newProgress);
}
private void openDialog(int newProgress) {
// TODO Auto-generated method stub
if(dialog == null) {
dialog = new ProgressDialog(MainActivity.this);
dialog.setTitle("正在加载");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(newProgress);
dialog.show();
} else {
dialog.setProgress(newProgress);
}
}
private void closeDialog() {
// TODO Auto-generated method stub
if(dialog != null && dialog.isShowing()) {
dialog.dismiss();
dialog = null;
}
}
});
以上是打开网页的两种基本方式,下面是慕课网上的一个实力,仅供参考
package com.imooc.android_webview;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
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.LayoutInflater;
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;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
private String url = "http://2014.qq.com/";
private WebView webView;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
// Uri uri = Uri.parse(url); //url为你要链接的地址
// Intent intent =new Intent(Intent.ACTION_VIEW, uri);
// startActivity(intent);
init();
}
private void init() {
// TODO Auto-generated method stub
webView = (WebView) findViewById(R.id.webView);
// WebView加载本地资源
// webView.loadUrl("file:///android_asset/example.html");
// WebView加载web资源
webView.loadUrl(url);
// 覆盖WebView默认通过第三方或者是系统浏览器打开网页的行为,使得网页可以在WebVIew中打开
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
//返回值是true的时候控制网页在WebView中去打开,如果为false调用系统浏览器或第三方浏览器去打开
view.loadUrl(url);
return true;
}
//WebViewClient帮助WebView去处理一些页面控制和请求通知
});
//启用支持JavaScript
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
//WebView加载页面优先使用缓存加载
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
//newProgress 1-100之间的整数
if(newProgress==100)
{
//网页加载完毕,关闭ProgressDialog
closeDialog();
}
else
{
//网页正在加载,打开ProgressDialog
openDialog(newProgress);
}
}
private void closeDialog() {
// TODO Auto-generated method stub
if(dialog!=null&&dialog.isShowing())
{
dialog.dismiss();
dialog=null;
}
}
private void openDialog(int newProgress) {
// TODO Auto-generated method stub
if(dialog==null)
{
dialog=new ProgressDialog(MainActivity.this);
dialog.setTitle("正在加载");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(newProgress);
dialog.show();
}
else
{
dialog.setProgress(newProgress);
}
}
});
}
//改写物理按键——返回的逻辑
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_BACK)
{
//Toast.makeText(this, webView.getUrl(), Toast.LENGTH_SHORT).show();
if(webView.canGoBack())
{
webView.goBack();//返回上一页面
return true;
}
else
{
System.exit(0);//退出程序
}
}
return super.onKeyDown(keyCode, event);
}
}
<?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/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.imooc.android_webview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.imooc.android_webview.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>