Use html for layout files in android

Source: Internet
Author: User

In android development, layout files are usually described in xml format. Currently, few people are familiar with android layout and beautification, and serious faults have occurred. In most enterprises, programmers are still working on their own. This will not only waste time and energy, but may not achieve the desired results. However, in enterprise-level android development, layout using html pages also has many advantages (for example, simple, most developers and artists are familiar with it to facilitate unified updates, management ). According to my understanding, many companies are already using this method for layout development. This may also be a trend.
Next, I will provide an example code for you to learn how to use html pages to layout android apps.

Package com. dazhuo. ui;
 
Import java. util. List;
 
Import org. json. JSONArray;
Import org. json. JSONObject;
 
Import com. dazhuo. domain. Person;
Import com. dazhuo. service. PersonService;
 
Import android. app. Activity;
Import android. content. Intent;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. util. Log;
Import android. webkit. WebView;
 
 
Public class MainActivity extends Activity {
Private PersonService service;
Private WebView webview;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Service = new PersonService ();
Webview = (WebView) this. findViewById (R. id. webView); // android built-in browser object
Webview. getSettings (). setJavaScriptEnabled (true); // enables javascript support.
// Add a js interaction interface to allow the javascript code in the html layout file to directly interact with the background java code.
Webview. addJavascriptInterface (new PersonPlugin (), "Person"); // new Class Name, alias used for interactive access
// <Body onload = "javascript: Person. getPersonList ()">
Webview. loadUrl ("file: // android_asset/index.html"); // load the local html layout file
// In fact, you can put this html layout file on the public network to facilitate updates and maintenance at any time, such as webview. loadUrl ("www.xxxx.com/index.html ");
}
// Define an internal class, obtain List set data from the java background (may be from the network, file or sqllite database), convert the data to a json string, and call the front-end js Code
Private final class PersonPlugin {
Public void getPersonList (){
List <Person> list = service. getPersonList (); // obtain the List data set
// Convert the data of the List generic set to the JSON Data Format
Try {
JSONArray arr = new JSONArray ();
For (Person person: list)
{
JSONObject json = new JSONObject ();
Json. put ("id", person. getId ());
Json. put ("name", person. getName ());
Json. put ("mobile", person. getMobile ());
Arr. put (json );

}
String JSONStr = arr. toString (); // convert to a json String
Webview. loadUrl ("javascript: show ('" + JSONStr + "')"); // execute the javascript function code in the html layout file --
Log. I ("MainActivity", JSONStr );
} Catch (Exception e ){
// TODO: handle exception
}

}
// Call Method
Public void call (String mobile ){
Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel:" + mobile ));
StartActivity (intent );
}
}
}

Package com. dazhuo. domain;
 
Public class Person {
Private Integer id;
Public Integer getId (){
Return id;
}
Public Person (Integer id, String name, String mobile ){
Super ();
This. id = id;
This. name = name;
This. mobile = mobile;
}
Public void setId (Integer id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getMobile (){
Return mobile;
}
Public void setMobile (String mobile ){
This. mobile = mobile;
}
Private String name;
Private String mobile;
}


Package com. dazhuo. service;
 
Import java. util. ArrayList;
Import java. util. List;
 
Import com. dazhuo. domain. Person;
 
Public class PersonService {
Public List <Person> getPersonList ()
{

List <Person> list = new ArrayList <Person> ();
List. add (new persons (32, "aa", "13675574545 "));
List. add (new Person (32, "bb", "13698874545 "));
List. add (new Person (32, "cc", "13644464545 "));
List. add (new Person (32, "dd", "13908978877 "));
List. add (new Person (32, "ee", "15908989898 "));
Return list;
}
}


<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
<Script type = "text/javascript">
Function show (jsondata ){
Var jsonobjs = eval (jsondata );
Var table = document. getElementById ("personTable ");
For (var y = 0; y <jsonobjs. length; y ++ ){
Var tr = table. insertRow (table. rows. length); // Add a row
// Add three columns
Var td1 = tr. insertCell (0 );
Var td2 = tr. insertCell (1 );
Td2.align = "center ";
Var td3 = tr. insertCell (2 );
Td3.align = "center ";
// Set the column content and attributes
Td1.innerHTML = jsonobjs [y]. id;
Td2.innerHTML = jsonobjs [y]. name;
Td3.innerHTML = "<a href = 'javascript: Person. call (\ "" + jsonobjs [y]. mobile + "\") '> "+ jsonobjs [y]. mobile + "</a> ";
}
}
</Script>
 
</Head>
<! -- Js Code uses webView to call the java code in its plug-in -->
<Body onload = "javascript: Person. getPersonList ()">
<Table border = "0" width = "100%" id = "personTable" cellspacing = "0">
<Tr>
<Td width = "20%"> NO. </td> <td width = "40%" align = "center"> name </td> <td align = "center"> phone number </td>
</Tr>
</Table>
<A href = "javascript: window. location. reload ()"> refresh </a>
</Body>
 
</Html>

From: A programmer in the programming world

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.