Android中获取网页表单中的数据实现思路及代码
程序员文章站
2023-01-02 08:46:49
mainactivity如下: 复制代码 代码如下: package cn.testjavascript; import java.util.stringtokenizer...
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>
复制代码 代码如下:
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>
上一篇: 酸奶和什么水果绝配,赶紧get起来吧!
推荐阅读
-
Android中获取网页表单中的数据实现思路及代码
-
android获取音乐文件的内置专辑图片实现思路及代码
-
用php实现的获取网页中的图片并保存到本地的代码
-
Oracle SQL中实现indexOf和lastIndexOf功能的思路及代码
-
Android中获取网页表单中的数据实现思路及代码
-
Extjs中TabPane如何嵌套在其他网页中实现思路及代码
-
用php实现的获取网页中的图片并保存到本地的代码_PHP
-
0x00000050电脑蓝屏代码 用php实现的获取网页中的图片并保存到本地的代码
-
用php实现的获取网页中的图片并保存到本地的代码
-
数据结构与算法1:二叉树(binary_tree)的前、中、后序(深度优先)和广度优先遍历及python代码实现