How to get the data in the form of Web page in Android and code _android

Source: Internet
Author: User
Mainactivity is as follows:
Copy Code code as follows:

Package cn.testjavascript;
Import Java.util.StringTokenizer;
Import Android.os.Bundle;
Import Android.webkit.WebView;
Import android.app.Activity;
/**
* Demo Description:
* Get the data from the form on the Web in 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");
Note the second parameter in the Addjavascriptinterface method
It represents the alias of our Java object Javaclass.
So that JavaScript can invoke the method in Android via this alias.
That is, in JavaScript code: Window.testform.send (date+ "|") +email+ "|" +name+ "|" +sex);
Send is a method name
Testform is an alias.
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 is as follows:
Copy Code code as follows:

<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 is as follows:
Copy Code code as follows:

<body>
<form action= "" method= "POST" >
Time:<br>
<select id= "Shijian" name= "Date" >
<option value= ">2011</option>"
<option value= ">2012</option>"
<option value= "2013" >2013</option>
<option value= "2014" >2014</option>
<option value= "2015" >2015</option>
</select><br>
Mailbox:
<input id= "Email" type= "text" name= "Emailid"/>
<br>
Nickname:
<input id= "name" type= "text" name= "username"/>
<br>
Gender:<br>
<input id= "Men" type= "Radio" name= "Sex" value= "men"/> Male
<input id= "Women" type= "Radio" name= "Sex" value= "women"/> Female
<br>
<input type= "Submit" value= "registered" onclick= "f ()"/>
<input type= "button" value= "Cancel"/>
</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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.