2013年8月9日星期五

Android to get the data in a web form realization of ideas and code

 
  Get in Android web page in the form of data specific implementation code below, you can refer interested in Kazakhstan over the next, we hope to help  
 
  MainActivity as follows:   
  
    copy the code the code below:   
  
   
package cn.testjavascript;    
import java.util.StringTokenizer;    
import android.os.Bundle;    
import android.webkit.WebView;    
import android.app.Activity;    
/ **    
* Demo Description:    
* to get started in the Android data in the form    
* /    
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 that the second parameter addJavascriptInterface method    
/ / it represents our java objects javaClass alias.    
/ / so that Javascript can be invoked via the alias method in Android    
/ / ie Javascript code: window.testform.send (date + "|" + email + "|" + name + "|" + sex);    
/ / send 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 (). 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:   
  
    copy the code the code below:   
  
   
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"    
>    
android: id = "@ + id / webView"    
android: layout_width = "fill_parent"    
android: layout_height = "fill_parent"    
android: layout_centerInParent = "true"    
/>    
  
  
form.html as follows:   
  
    copy the code the code below:   
  
   
   
   
Time:
   

   
E-mail:    
   

   
Nickname:    
   

   
Gender:
   
male    
female    

   
   
   
   
   
  
 

没有评论:

发表评论