How to use HTML as a layout file in Android _android

Source: Internet
Author: User

In Android development, you typically use an XML format to describe a layout file. For now, there are so few people familiar with Android layouts and landscaping that there are serious faults. Most businesses, in fact, are programmers themselves. This is a waste of time and energy, and may not be able to achieve the desired effect. However, in the enterprise-level Android development, the use of HTML page layout, there are many advantages (such as: simple, most developers and artists are familiar with, easy to unify the update, management). As far as I know, there are already a lot of companies in the use of this way layout development. This may also be a trend.

Next, I'll give you an example code to learn how to use HTML pages to apply the layout to Android.

Copy Code code as follows:

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);/enable JavaScript support
Add a JS interface to facilitate the HTML layout file JavaScript code can be directly interactive with the background Java code access
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")//loading local HTML layout file
In fact, this HTML layout file can be placed in the public network, so easy to update maintenance at any time such as Webview.loadurl ("www.xxxx.com/index.html");
}
Define an inner class that gets the list collection data from the Java backend (possibly from a network, file, or Sqllite database) and converts it into a JSON string, calling the foreground JS code
Private Final Class personplugin{
public void Getpersonlist () {
list<person> list = Service.getpersonlist ();//Get List data collection
Converts the data in a list generic collection to 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 JSON string
Webview.loadurl ("javascript:show (' + jsonstr + ')")//execute JavaScript function code in HTML layout file--
LOG.I ("Mainactivity", jsonstr);
catch (Exception e) {
Todo:handle exception
}

}
How to make a phone call
public void Call (String mobile) {
Intent Intent = new Intent (Intent.action_call, Uri.parse ("Tel:" + mobile));
StartActivity (Intent);
}
}
}


Copy Code code as follows:

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;
}


Copy Code code as follows:

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 person ("AA", "13675574545"));
List.add (New person ("BB", "13698874545"));
List.add (New person ("CC", "13644464545"));
List.add (New person ("DD", "13908978877"));
List.add (New person ("ee", "15908989898"));
return list;
}
}


Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<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 column contents and properties
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>

<!--JS code calls the Java code in its plug-in via WebView-->
<body onload= "javascript:Person.getPersonList ()" >
<table border= "0" width= "100%" id= "persontable" cellspacing= "0" >
<tr>
<TD width= "20%" > Number </td><td width= "40%" align= "center" > Name </td><td align= "center" > Phone </td>
</tr>
</table>
<a href= "Javascript:window.location.reload ()" > Refresh </a>
</body>

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.