当前位置:操作系统 > 安卓/Android >>

Android使用TableLayout动态布局实例

有图有真相,请先看效果截图:

\

实现主要功能:
 * 1.使用TableLayout动态布局展示,可动态添加和删除.
 * 2.初始化时显示动态展示,初始化的数据改造后可来自数据库.
 * 3.重置时到初始化状态.
 * 4.保存时去重检查,参见代码中去重算法.
首先,建立实体类:
[java]
<span style="font-size:18px;">package tgb.lk.tablelayout; 
 
public class Dict { 
 
    private int id; 
    private String name; 
    private int orders; 
    private String desc; 
    private int types; 
     
    //get,set方法. 
 
    @Override 
    public String toString() { 
        return "Dict [id=" + id + ", name=" + name + ", orders=" + orders 
                + ", types=" + types + "]"; 
    } 
 

</span> 

其次,建立数据层方法接口:
[java]
<span style="font-size:18px;">package tgb.lk.tablelayout; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import android.content.Context; 
 
public class DictDaoImpl { 
 
    public DictDaoImpl(Context context) { 
 
    } 
 
    public boolean isExist() { 
        return false; 
    } 
 
    public List<Dict> getDictItem(String dictId) { 
        String[] dicts = new String[] { "华东", "华南", "华北", "华中", "西南", "东北" }; 
        List<Dict> retList = new ArrayList<Dict>(); 
        for (int j = 0; j < dicts.length; j++) { 
            Dict dict = new Dict(); 
            dict.setName(dicts[j]); 
            dict.setId(j); 
            dict.setOrders(j); 
            dict.setTypes(1000); 
            retList.add(dict); 
        } 
        return retList; 
    } 
 
    public long insert(Dict entity) { 
        return 1L; 
    } 
 
    public void delete(int id) { 
    } 
 
    public void update(Dict entity) { 
    } 
 
    public Dict get(Object id) { 
        Dict dict = new Dict(); 
        dict.setId(1); 
        dict.setName("华东"); 
        dict.setOrders(1); 
        dict.setTypes(1000); 
        return dict; 
    } 
 
    public void delete(Integer... ids) { 
    } 

</span> 

然后,建立layout布局dict_item.xml:
[html]
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars="none" > 
 
    <LinearLayout 
        android:id="@+id/dictLayout" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" 
        android:scrollbars="" > 
 
        <TextView 
            android:id="@+id/title" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="业务字典管理" /> 
 
        <LinearLayout 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:orientation="horizontal" > 
 
            <TextView 
                android:layout_width="80dp" 
                android:layout_height="wrap_content" 
                android:text="名称:" /> 
 
            <EditText 
                android:id="@+id/name" 
                android:layout_width="200dp" 
                android:layout_height="wrap_content" 
                android:hint="请输入业务字典名称" 
                android:maxLength="20" 
                android:singleLine="true" /> 
        </Linear

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,