当前位置: 编程技术>移动开发
Android中获取网页表单中的数据实现思路及代码
来源: 互联网 发布时间:2014-10-20
本文导语: MainActivity如下: 代码如下: package cn.testjavascript; import java.util.StringTokenizer; import android.os.Bundle; import android.webkit.WebView; import android.app.Activity; /** * Demo描述: * 在Android中获取网页里表单中的数据 */ public class MainActivity extends Activity { ...
MainActivity如下:
package cn.testjavascript;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;
/**
* Demo描述:
* 在Android中获取网页里表单中的数据
*/
public class MainActivity extends Activity {
private WebView mWebView;
private String date =null;
private String email = null;
private String username = null;
private String sex = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mWebView=(WebView) findViewById(R.id.webView);
initWebViewSettings();
mWebView.loadUrl("file:///android_asset/form.html");
//注意addJavascriptInterface方法中第二参数
//它表示我们的java对象javaClass的别名.
//这样Javascript就可以通过该别名来调用Android中的方法
//即Javascript代码中的:window.testform.send(date+"|"+email+"|"+name+"|"+sex);
//send是方法名
//testform是别名
mWebView.addJavascriptInterface(new Object() {
public void send(String userInfo) {
StringTokenizer userInfoStringTokenizer = new StringTokenizer(userInfo, "|");
date = userInfoStringTokenizer.nextToken();
email = userInfoStringTokenizer.nextToken();
username = userInfoStringTokenizer.nextToken();
sex = userInfoStringTokenizer.nextToken();
System.out.println("userInfoStringTokenizer="+userInfoStringTokenizer.toString());
System.out.println("date=" + date);
System.out.println("email=" + email);
System.out.println("username=" + username);
System.out.println("sex=" + sex);
};
}, "testform");
}
private void initWebViewSettings(){
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.requestFocus();
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
}
}
main.xml如下:
form.html如下:
时间:
2011
2012
2013
2014
2015
邮箱:
昵称:
性别:
男
女
function f(){
var email = document.getElementById('email').value;
var name = document.getElementById('name').value;
var date = document.getElementById('shijian').value;
if(document.getElementById('men').checked && !document.getElementById('women').checked){
var sex = document.getElementById('men').value;
}else if(!document.getElementById('men').checked && document.getElementById('women').checked){
var sex = document.getElementById('women').value;
}
window.testform.send(date+"|"+email+"|"+name+"|"+sex);
}
代码如下:
package cn.testjavascript;
import java.util.StringTokenizer;
import android.os.Bundle;
import android.webkit.WebView;
import android.app.Activity;
/**
* Demo描述:
* 在Android中获取网页里表单中的数据
*/
public class MainActivity extends Activity {
private WebView mWebView;
private String date =null;
private String email = null;
private String username = null;
private String sex = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mWebView=(WebView) findViewById(R.id.webView);
initWebViewSettings();
mWebView.loadUrl("file:///android_asset/form.html");
//注意addJavascriptInterface方法中第二参数
//它表示我们的java对象javaClass的别名.
//这样Javascript就可以通过该别名来调用Android中的方法
//即Javascript代码中的:window.testform.send(date+"|"+email+"|"+name+"|"+sex);
//send是方法名
//testform是别名
mWebView.addJavascriptInterface(new Object() {
public void send(String userInfo) {
StringTokenizer userInfoStringTokenizer = new StringTokenizer(userInfo, "|");
date = userInfoStringTokenizer.nextToken();
email = userInfoStringTokenizer.nextToken();
username = userInfoStringTokenizer.nextToken();
sex = userInfoStringTokenizer.nextToken();
System.out.println("userInfoStringTokenizer="+userInfoStringTokenizer.toString());
System.out.println("date=" + date);
System.out.println("email=" + email);
System.out.println("username=" + username);
System.out.println("sex=" + sex);
};
}, "testform");
}
private void initWebViewSettings(){
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.requestFocus();
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setBuiltInZoomControls(true);
}
}
main.xml如下:
代码如下:
form.html如下:
代码如下:
时间:
2011
2012
2013
2014
2015
邮箱:
昵称:
性别:
男
女
function f(){
var email = document.getElementById('email').value;
var name = document.getElementById('name').value;
var date = document.getElementById('shijian').value;
if(document.getElementById('men').checked && !document.getElementById('women').checked){
var sex = document.getElementById('men').value;
}else if(!document.getElementById('men').checked && document.getElementById('women').checked){
var sex = document.getElementById('women').value;
}
window.testform.send(date+"|"+email+"|"+name+"|"+sex);
}