代码
package com.example.webviewdemo1409s;
import java.net.URLEncoder;
import java.security.spec.EncodedKeySpec;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
public class MainActivity extends Activity {
private WebView mWebView;
private ProgressDialog mDlg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// setContentView(new WebView(this));
// 从布局文件中查找webview
mWebView = (WebView) findViewById(R.id.web);
// 加载本地资源
// mWebView.loadUrl("file:///android_asset/test.html");
// 加载远程资源
String data = "rdgdgdgd
百度";
String mimeType = "text/html; charset=utf-8";
// mWebView.loadData(data, mimeType , null);
// 直接可以解决中文乱码的问题
// mWebView.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
// 获取webView设置对象
WebSettings settings = mWebView.getSettings();
settings.setBuiltInZoomControls(true);
// setPluginEnable已经弃用了
settings.setPluginsEnabled(false);
settings.setJavaScriptEnabled(true);
// 使用webSettings.setPluginState(WebSettings.PluginState.ON);可以代替,
// 但是这个方法在API18之后也弃用了,因为adobe不再开发新的flash移动版了
settings.setPluginState(WebSettings.PluginState.ON);
// 设置加载
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// 加载页面时
super.onPageStarted(view, url, favicon);
if (mDlg == null) {
mDlg = ProgressDialog.show(MainActivity.this, "提示", "加载……");
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
// 关闭加载框
mDlg.dismiss();
}
@Override
public void onPageFinished(WebView view, String url) {
// 页面加载完成时执行
super.onPageFinished(view, url);
// 关闭加载框
mDlg.dismiss();
}
});
mWebView.setWebChromeClient(new WebChromeClient(){
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
//提示框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示框");
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
builder.show();
return true;
}
@Override
public void onReceivedTitle(WebView view, String title) {
//接受标题
super.onReceivedTitle(view, title);
}
@Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
//确认框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示框");
builder.setMessage(message);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();;
}
});
builder.show();
// URLEncoder.encode(s)
return true;
}
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
final JsPromptResult result) {
//确认框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("输入框");
builder.setMessage(message);
//输入框
final EditText et = new EditText(MainActivity.this);
builder.setView(et);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm(et.getText().toString());
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.cancel();;
}
});
builder.show();
return true;
}
});
mWebView.addJavascriptInterface(new JSInterface(), "jsobj");
mWebView.loadUrl("file:///android_asset/test.html");
}
class JSInterface {
@JavascriptInterface
public String getName(){
return "陈振国";
}
@JavascriptInterface
public void callJS(){
mWebView.loadUrl("javascript:show();");
}
}
}