Android 採用HTML設計介面

來源:互聯網
上載者:User

Android 採用HTML設計介面

  因為Android軟體開發分工目前還沒有細化,程式員往往需要負責軟體介面的開發,雖然軟體的介面圖片已經由美工設計好了,但是如果使用layout技術把軟體做成漂亮的介面確實很困難,而是也比較耗時。Android通過WebView實現了JS代碼與Java代碼互相通訊的功能,使的Android軟體的介面開發也可以採用HTML網頁技術,這樣,廣大網頁美工可以參與進Android軟體的介面開發工作,從而讓程式員從中解脫出來。  

工程目錄

main.xml

    

index.html

<script type="text/javascript"> function show(jsondata){// [{name:"xxx",amount:600,phone:"13988888"},{name:"bb",amount:200,phone:"1398788"}] var jsonobjs = eval(jsondata); var table = document.getElementById("personTable");//找到personTable表 for(var y=0; y
姓名 存款 電話
重新整理

Contact.java<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">package cn.itcast.domain;public class Contact { private Integer id; private String name; private String phone; private Integer amount; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Integer getAmount() { return amount; } public void setAmount(Integer amount) { this.amount = amount; } public Contact(Integer id, String name, String phone, Integer amount) { this.id = id; this.name = name; this.phone = phone; this.amount = amount; }}

ContactService.java

package cn.itcast.service;import java.util.ArrayList;import java.util.List;import cn.itcast.domain.Contact;public class ContactService {    /**     * 擷取連絡人     * @return     */    public List getContacts(){        List contacts = new ArrayList();        contacts.add(new Contact(12, "張明", "13766666666", 13003));        contacts.add(new Contact(23, "小小", "130066006", 122003));        contacts.add(new Contact(98, "李小楷", "186768768", 10988787));        contacts.add(new Contact(76, "趙得", "1565622566", 1666));        return contacts;    }}

MainActivity.java

package cn.itcast.html;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import cn.itcast.domain.Contact;import cn.itcast.service.ContactService;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.webkit.WebView;public class MainActivity extends Activity {    private WebView webView;    private ContactService contactService;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        webView = (WebView) this.findViewById(R.id.webView);        webView.loadUrl("file:///android_asset/index.html");        webView.getSettings().setJavaScriptEnabled(true);        webView.addJavascriptInterface(new JSObject(), "contact");//js對象是 JSObject(),名稱為contact        contactService = new ContactService();    }    private final class JSObject{        public void call(String phone){            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phone));            startActivity(intent);        }        public void showcontacts(){            //  [{name:"xxx",amount:600,phone:"13988888"},{name:"bb",amount:200,phone:"1398788"}]            try {                List contacts = contactService.getContacts();                JSONArray jsonArray = new JSONArray();                for(Contact c : contacts){                    JSONObject jsonObject = new JSONObject();                    jsonObject.put("name", c.getName());                    jsonObject.put("amount", c.getAmount());                    jsonObject.put("phone", c.getPhone());                    jsonArray.put(jsonObject);                }                String json = jsonArray.toString();                webView.loadUrl("javascript:show('"+ json+ "')");            } catch (JSONException e) {                e.printStackTrace();            }        }    }}

測試結果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.