Java+html+javascript Hybrid development _android of Android Development mode

Source: Internet
Author: User

Android Development, in addition to the use of the original eco-development approach, can also be developed using Java+html+javascript hybrid development, which can save a lot of development time, but also to enable users of different devices to obtain the same user experience. OK, no more nonsense, first come to see what to do today.
The main is to achieve a simple registration function, the first use of jquery mobile way to write a simple registration page, click the Submit button to jump to a new activity, while the user's registration information displayed, the overall effect of the following figure:

This page is completely written in Html+jquery, and it has a submit button at the bottom, and after clicking the Submit button, all the registration information for the page is passed to the next activity.

This interface is developed entirely from the original Android ecosystem. OK, here's a step-by-step step.

To create a new project called Webviewtest, create a new file in the assets folder called the index.html,index.html file code as follows:

<!doctype html>  

All of this is jquerymobile knowledge, and the first three lines are the JS files that refer to jquery and jquerymobile and jquerymobile CSS style files. When the button is clicked, execute the JS code, the JS code gets the value of each item while piecing together into a JSON string, then executes register_js.register (result); method, what is this method? This is a method of loading this HTML activity called Register, result is the parameter of this method, as for the front why is REGISTER_JS, we will say later.

To see what the activity of loading this HTML looks like, first look at its layout file:

<linearlayout 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:orientation= "vertical"
 tools:context= "com.example.webviewtest.MainActivity" >

 < WebView 
  android:id= "@+id/wv1"
  android:layout_width= match_parent "android:layout_height=" Wrap_
  Content "
  />

</LinearLayout>

Its layout file is in one control, WebView.

Look at the Java code again:

 package com.example.webviewtest;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;

Import Android.webkit.WebView;
 public class Mainactivity extends activity {private WebView WV;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

  Setcontentview (R.layout.activity_main);
  WV = (WebView) This.findviewbyid (R.ID.WV1);
  Wv.getsettings (). Setjavascriptenabled (True);
  Wv.loadurl ("file:///android_asset/index.html");
 Wv.addjavascriptinterface (This, "register_js");
  The public void register (String userInfo) {Intent Intent = new Intent (mainactivity.this,registeractivity.class);
  Intent.putextra ("UserInfo", userinfo);
 This.startactivity (Intent); }
}

Get a webview first, then Wv.getsettings (). Setjavascriptenabled (True), which indicates that JS code is allowed to execute, wv.loadurl ("file:///android_asset/ Index.html"); To load the HTML file just in, note the file path, the project is a assets folder, not Android_assets,wv.addjavascriptinterface (this," Register_js "); represents the creation of an object passed to WebView as a JS object, where the activity is passed to the WebView, named Register_js, So in the JS in the implementation of this activity in the method in front to add register_js, of course, you can pass any instance of a class as a JS object, so you can call the class in JS method. The public void register (String userInfo) method is the method that is executed when you click the Submit button in HTML, which jumps the execution to another activity and carries the user registration data.

Let's look at the registeractivity layout file:

<linearlayout 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:o" rientation= "vertical" tools:context= "com.example.webviewtest.MainActivity" > <textview android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "registered successfully, your registration information is:" android:textsize= "30DP"/> &L T LinearLayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Horizontal "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android : text= "username:" android:textsize= "25sp"/> <textview android:id= "@+id/username" android:layout_width= "WRAP_" Content "android:layout_height=" wrap_content "android:textsize=" 25sp "/> </LinearLayout> <linearlayou T android:layout_width= "match_parent" android:layout_height= "WRAp_content "android:orientation=" horizontal "> <textview android:layout_width=" wrap_content "Android:layo" ut_height= "wrap_content" android:text= "Password:" android:textsize= "25sp"/> <textview android:id= "@+id/passwo Rd "Android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:textsize=" 25sp "/> </ linearlayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android : orientation= "Horizontal" > <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_conte" NT "android:text=" Sex: "Android:textsize=" "25sp"/> <textview android:id= "@+id/gender" Android:layout_w Idth= "Wrap_content" android:layout_height= "wrap_content" android:textsize= "25sp"/> </LinearLayout> < LinearLayout android:layout_width= "match_parent" android:layout_height= "Wrap_content"
Horizontal "> <textview   Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "hobby:" android:textSize= " 25sp "/> <textview android:id=" @+id/interest android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:textsize= "25sp"/> </LinearLayout> <linearlayout android:layout_width= "Match_pa" Rent "android:layout_height=" wrap_content "android:orientation=" horizontal "> <textview android:layout_wid Th= "Wrap_content" android:layout_height= "wrap_content" android:text= "Nationality:" android:textsize= "25sp"/> <Te Xtview android:id= "@+id/country" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Andr

 Oid:textsize= "25sp"/> </LinearLayout> </LinearLayout>

Java code for registeractivity:

Package com.example.webviewtest;
Import org.json.JSONException;

Import Org.json.JSONObject;
Import android.app.Activity;
Import Android.os.Bundle;

Import Android.widget.TextView;

 public class Registeractivity extends activity {Private TextView username, password, interest, country, gender;
  @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  This.setcontentview (r.layout.register_activity);
  This.username = (TextView) This.findviewbyid (r.id.username);
  This.password = (TextView) This.findviewbyid (R.id.password);
  This.interest = (TextView) This.findviewbyid (r.id.interest);
  This.country = (TextView) This.findviewbyid (r.id.country);
  This.gender = (TextView) This.findviewbyid (R.id.gender);
  String userinfo = This.getintent (). Getextras (). getString ("userinfo");
   try {jsonobject json = new Jsonobject (userinfo);
   Username.settext (json.getstring ("username"));
   Password.settext (json.getstring ("password")); Interest.sEttext (json.getstring ("interest"). Replace ("0", "Football"). Replace ("1", "basketball"). Replace ("2", "volleyball"));
   Country.settext (json.getstring ("Country"). Replace ("0", "China"). Replace ("1", "USA"). Replace ("2", "Small Japan"));
  Gender.settext (json.getstring ("Gender"). Replace ("0", "male"). Replace ("1", "female"));
  catch (Jsonexception e) {e.printstacktrace ();

 }
 }
}

These are the regular Android development code, and I don't explain it much.
In addition, add the following permissions to the layout file:

<uses-permission android:name= "Android.permission.CAMERA"/> <uses-permission android:name= " Android.permission.VIBRATE "/> <uses-permission android:name=" Android.permission.ACCESS_COARSE_LOCATION " > <uses-permission android:name= "Android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android: Name= "Android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/> <uses-permission android:name= " 

Android.permission.READ_PHONE_STATE "/> <uses-permission android:name=" Android.permission.INTERNET "/>" <uses-permission android:name= "Android.permission.RECEIVE_SMS"/> <uses-permission android:name= " Android.permission.RECORD_AUDIO "/> <uses-permission android:name=" Android.permission.MODIFY_AUDIO_SETTINGS "/> <uses-permission android:name=" Android.permission.READ_CONTACTS "/> <uses-permission" Android.permission.WRITE_CONTACTS "/> <uses-permission android:name=" Android.permission.WRITE_EXTERnal_storage "/> <uses-permission android:name= Android.permission.ACCESS_NETWORK_STATE"/> <

 Uses-permission android:name= "Android.permission.GET_ACCOUNTS"/>

About the mixed development of this piece involves too much content, I will be writing the article later.

Original link: http://blog.csdn.net/u012702547/article/details/45727329

SOURCE Download: Http://xiazai.jb51.net/201606/yuanma/webViewTest (jb51.net). rar

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.