The specific implementation code for getting the data in the form on the webpage in Android is as follows. If you are interested, refer to the following code. I hope to help you with MainActivity as follows:
The Code is 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 in the form on the webpage 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.
// In this way, Javascript can use this alias to call methods in Android.
// Window. testform. send (date + "|" + email + "|" + name + "|" + sex) in Javascript code );
// Send is the 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 (). setsuppzoom zoom (true );
MWebView. getSettings (). setDomStorageEnabled (true );
MWebView. getSettings (). setPluginsEnabled (true );
MWebView. requestFocus ();
MWebView. getSettings (). setUseWideViewPort (true );
MWebView. getSettings (). setLoadWithOverviewMode (true );
MWebView. getSettings (). setsuppzoom zoom (true );
MWebView. getSettings (). setBuiltInZoomControls (true );
}
}
Main. xml is as follows:
The Code is 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:
The Code is as follows:
<Body>
<Form action = "" method = "post">
Time: <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>
Email:
<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 = "register" 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>