WebView
package com.baidu.day09_webview;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String HTML_URL = "";
private WebView webView;
class MyClass1 {
/*
* **********@必须添加*********
*/
@JavascriptInterface
public void senData(String name, String pass, String email) {
Toast.makeText(MainActivity.this, name + "," + pass + "," + email,
0).show();
// 网络连接,发送到服务器保存,同时也可以保存到本地sqlite
// 跳转到注册成功页面
// Intent it = new Intent(mcontext,第二个页面)E;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
@SuppressLint("SetJavaScriptEnabled")
private void init() {
// TODO Auto-generated method stub
TextView textView = (TextView) findViewById(R.id.tv);
webView = (WebView) findViewById(R.id.wv);
Button button = (Button) findViewById(R.id.button1);
Button tj = (Button) findViewById(R.id.tj);
tj.setOnClickListener(new OnClickListener() {
// 点击提交,通过android调用的jscheckAll函数
public void onClick(View v) {
// TODO Auto-generated method stub
// 调用javascript中的代码
webView.loadUrl("javascript:checkAll('android调用的验证')");
}
});
// 获得websettings,设置一些参数
WebSettings settings = webView.getSettings();
// 设置编码,解决乱码
settings.setDefaultTextEncodingName("utf-8");
// 设置普通js有效
settings.setJavaScriptEnabled(true);
/*
* 两种方式加载html
*/
// 加载服务器html资源
// webView.loadUrl(HTML_URL);
// 加载本地assets下的html资源
webView.loadUrl("file:///android_asset/myHtml.html");
// 向服务器插入一个对象供调用
webView.addJavascriptInterface(new MyClass1(), "myclass");
// 监听webview的url的重新定位
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
Log.i("TAG", "当前webview加载的新url:::" + url);
// if(url.equals("http://www.baidu.com")){
// webView.loadUrl("http://www.jd.com");
// }
return super.shouldOverrideUrlLoading(view, url);
}
});
// 处理alert无效
webView.setWebChromeClient(new WebChromeClient() {
// 处理alert无效,当js弹出alert对话框的时候会调用
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
// 用dialog对话框代替系统自带的alert对话框。先关闭
result.cancel();
// 创建android的对话框进行替换
AlertDialog.Builder dialog = new AlertDialog.Builder(
MainActivity.this);
dialog.setTitle("警告框").setIcon(R.drawable.ic_launcher)
.setMessage(message).setPositiveButton("确定", null)
.create().show();
return true;
}
// 处理confirm无效,当js弹出confirm对话框的时候,调用
@Override
public boolean onJsConfirm(WebView view, String url,
String message, final JsResult result) {
// TODO Auto-generated method stub
// 创建android对话框替换
AlertDialog.Builder dialog = new AlertDialog.Builder(
MainActivity.this);
dialog.setTitle("确认提示框")
.setIcon(R.drawable.ic_launcher)
.setMessage(message)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
result.confirm();// 点击dialog确认,提交系统的确认框
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
result.cancel();// 点击dialog取消,提交系统的确认框
}
}).create().show();
return true;
}
});
webView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (webView.canGoBack()) {
webView.goBack();// 后退
// webView.goForward();// 前进
// webView.zoomIn();// 放大
// webView.zoomOut();// 缩小
}
}
});
/*
* 第二种方式,加载先拼写的html
*/
// StringBuffer sb = new StringBuffer();
// sb.append("<html><head><body><input type='submit' value='提交(app)'></body>></head></html>");
// webView.loadData(sb.toString(), "text/html", "utf-8");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}