Get the data implementation ideas and code in the web page form in Android

  • 2020-05-17 06:20:55
  • OfStack

MainActivity is as follows:
 
package cn.testjavascript; 
import java.util.StringTokenizer; 
import android.os.Bundle; 
import android.webkit.WebView; 
import android.app.Activity; 
/** 
* Demo describe : 
*  in Android Get the data from the form in the web page  
*/ 
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"); 
// Pay attention to addJavascriptInterface Methods the first 2 parameter  
// It represents our theta java object javaClass The alias . 
// such Javascript Can be called through this alias Android The methods in  
// namely Javascript In the code :window.testform.send(date+"|"+email+"|"+name+"|"+sex); 
//send Is the method name  
//testform Is the 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 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 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 address:  
<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 articles: