Android中获取网页表单中的数据实现思路及代码
时间:2021-07-04 19:51:18
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如下:
<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"
>
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
/>
</RelativeLayout>
form.html如下:
<body>
<form action="" method="post">
时间:<br>
<select id="shijian" name="date">
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select><br>
邮箱:
<input id="email" type="text" name="emailID" />
<br>
昵称:
<input id="name" type="text" name="username" />
<br>
性别:<br>
<input id="men" type="radio" name="sex" value="men"/>男
<input id="women" type="radio" name="sex" value="women"/>女
<br>
<input type="submit" value="注册" onclick="f()"/>
<input type="button" value="取消" />
</form>
</body>
<script type="text/JavaScript" language="javascript">
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);
}
</script>
标签:网页,表单数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Java实现面向对象编程java oop
2022-12-26 03:45:29
![](https://img.aspxhome.com/file/2023/7/88197_0s.png)
springboot实现定时任务的四种方式小结
2021-10-20 20:38:06
springboot接收别人上传的本地视频实例代码
2023-03-22 01:55:39
java新手入门——String类详解
2022-02-23 08:17:26
![](https://img.aspxhome.com/file/2023/6/70576_0s.png)
Java读取.properties配置文件方法示例
2023-08-24 16:32:56
![](https://img.aspxhome.com/file/2023/9/58159_0s.png)
c#基础学习之封装
2023-12-06 16:45:21
详谈Java几种线程池类型介绍及使用方法
2023-10-13 03:27:30
![](https://img.aspxhome.com/file/2023/1/92671_0s.jpg)
Java中流的有关知识点详解
2022-07-21 15:38:21
mybatis新手快速入门以及一些错误汇总
2023-01-25 13:01:14
![](https://img.aspxhome.com/file/2023/8/63798_0s.png)
使用Linq注意事项避免报错的方法
2023-08-28 21:22:41
![](https://img.aspxhome.com/file/2023/7/82157_0s.png)
Android实现今日头条订阅频道效果
2021-10-29 23:30:00
C#使用Lambda表达式简化代码的示例详解
2022-09-16 03:03:36
![](https://img.aspxhome.com/file/2023/8/88368_0s.png)
java开发分布式服务框架Dubbo原理机制详解
2023-01-04 19:53:01
![](https://img.aspxhome.com/file/2023/0/71210_0s.png)
java compare compareTo方法区别详解
2022-06-26 08:13:55
基于Mybatis映射的一点心得(分享)
2023-08-08 13:15:53
Java import导入及访问控制权限修饰符原理解析
2023-08-17 21:42:59
Android 8.0升级不跳转应用安装页面的解决方法
2022-03-20 03:30:13
![](https://img.aspxhome.com/file/2023/4/88094_0s.jpg)
C#中using关键字的使用方法示例
2023-07-30 14:17:02
![](https://img.aspxhome.com/file/2023/2/76392_0s.jpg)
MyBatis中的模糊查询语句
2022-09-30 03:36:59
Java读取json数据并存入数据库的操作代码
2023-09-23 06:00:57