代码直接下载http://download.csdn.net/detail/metis100/8514837
第一步,xml Manifest中要设置网络权限,否则会出先 webpage not available
<uses-permission android:name="android.permission.INTERNET" />
设置布局R.layout.activity_main。这里加了个自己定义进度条
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/webview_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#999999"
android:progressDrawable="@drawable/progress_custom_horizontal" />
</RelativeLayout>
这里@drawable/progress_custom_horizontal为自己定义控件
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 设置背景色图像资源 -->
<item android:id="@android:id/background">
<clip>
<shape>
<solid android:color="#999999" />
<corners android:radius="4dip" />
</shape>
</clip>
</item>
<!-- 设置第二级进度条颜色图像资源 -->
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<solid android:color="#217fbc" />
<corners android:radius="4dip" />
</shape>
</clip>
</item>
<!-- 设置第一级进度条颜色图像资源 -->
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#007eff" />
<corners android:radius="4dip" />
</shape>
</clip>
</item>
</layer-list>
接下来就是webview的设置,这里以快递100,为样例
package com.example.webview;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends Activity {
public WebView mWebView;
private WebSettings webSettings;
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mProgressBar = (ProgressBar) findViewById(R.id.webview_progress);
// 通过loadUrl方法设置当前webView须要訪问的网址:
mWebView.loadUrl("http://m.kuaidi100.com/index_all.html");
// 载入URL assets文件夹下的内容能够用 "file:///android_asset" 前缀
// mWebView.loadUrl("file:///android_asset/about.html");
// 在android中专门通过WebSettings来设置WebView的一些属性、状态等。在创建WebView时,
// 系统有一个默认的设置,我们能够通过WebView.getSettings来得到这个设置:
// WebSettings和WebView都在同一个生命周期中存在。当WebView被销毁后。假设再使用WebSettings,则会抛出异常。
webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);//设置支持JavaScript脚本
webSettings.setSupportZoom(false);//设置支持缩放
webSettings.setDomStorageEnabled(true);
mWebView.requestFocus();//使页面获得焦点
mWebView.setWebViewClient(mWebViewClient);//设置WebViewClient
mWebView.setWebChromeClient(mWebChromeClient);// 创建WebViewChromeClient
mWebView.addJavascriptInterface(new WDScriptInterface(), "FFTest");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
final class WDScriptInterface {
WDScriptInterface() {}
@JavascriptInterface
public void Call(String num) {
Intent in_sys_call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + num));
startActivity(in_sys_call);
}
}
//////////////////////////////////////////////////////////////////////////////////
//使用WebViewClient来完毕在应用程序中自己定义网页浏览程序:
//WebViewClient是辅助WebView处理各种通知、请求等事件的类。通过WebView的setWebViewClient方法指定WebViewClient对象。
//WebView能够通过覆盖WebViewClient方法来辅助WebView浏览网页:
WebViewClient mWebViewClient = new WebViewClient() {
// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转。不跳到浏览器那边
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("wocaonima", "shouldOverrideUrlLoading--url:" + url);
// 拨打电话
if (url.startsWith("tel:")) {
Intent in_sys_call = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(in_sys_call);
}
else {
view.loadUrl(url);
}
return true;
}
// 重写此方法能够让webview处理https请求
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, android.net.http.SslError error) {
handler.proceed();
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(MainActivity.this, "网络连接失败", 300);
};
};
//////////////////////////////////////////////////////////////////////////////
////创建WebViewChromeClient
WebChromeClient mWebChromeClient = new WebChromeClient() {
public void onProgressChanged(WebView view, int newProgress) {
mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(newProgress * 100);
if (newProgress == 100) mProgressBar.setVisibility(View.GONE);
};
// 处理javascript的alert
public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
// 构建一个Builder来显示网页中的alert对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示");
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
builder.setCancelable(false);
builder.create();
builder.show();
return true;
};
// 处理javascript的confirm
public boolean onJsConfirm(WebView view, String url, String message, final android.webkit.JsResult result) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示");
builder.setMessage(message);
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
builder.setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
builder.setCancelable(false);
builder.create();
builder.show();
return true;
};
// 输入框
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
final android.webkit.JsPromptResult result) {
final AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
builder.setTitle(message);
final EditText et = new EditText(view.getContext());
et.setSingleLine();
et.setText(defaultValue);
builder.setView(et);
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm(et.getText().toString());
}
}).setNeutralButton(android.R.string.cancel, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();
}
});
builder.setCancelable(false);
builder.create();
builder.show();
return true;
};
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
很多其它交流可加Android技术讨论群:71262831 或关注微信公众号:it达人
时间: 2024-10-10 05:42:49