1.创建程序
创建一个名为“商品展示”的应用程序,将包名修改为cn.edu.bzu。
2.用户交互界面
(1)“商品展示”程序对应的布局文件(activity_main.xml)
线性布局下包含一个水平显示的线性布局和一个ListView控件垂直显示;在水平显示的线性布局(LinearLayout)下建两个文本控件(EditText)“商品名称”、“金额”和一个显示图片控件。文本属性Android:inputType其中使用到了ImageView的属性Android:src来指定ImageView要显示的图片,但是只显示图片原图大小;如果使用Android:src属性,图片的大小会根据ImageView的大小进行拉伸。此外,ListView控件以列表的形式展示具体数据的内容,并且能够根据数据的长度自适应屏幕的显示。例:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="8dp" tools:context="cn.edu.bzu.showproduct.MainActivity"> <LinearLayout android:id="@+id/addLL" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/nameET" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="商品名称" android:inputType="textPersonName"/> <EditText android:id="@+id/balanceET" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="金额" android:inputType="number"/> <ImageView android:onClick="add" android:layout_width="30dp" android:layout_height="32dp" android:src="@drawable/input" /> </LinearLayout> <ListView android:id="@+id/accoutLV" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/addLL"> </ListView> </LinearLayout>
(2)ListView Item 布局(item.xml)
用到了ListView布局,故要编写一个ListView itemd的布局。在res/layout目录下建一个item.xml文件,该文件有三个TextView文本控件,分别用于显示数据库中的某条数据的id、商品名称、金额和三个ImageView图片控件用于增加、减少金额,删除数据。例: