实训一 用户界面设计
一、【实训目的】
(1) 用户界面基础
(2) 常用控件
(3) 提交实训报告和实训源码
二、【实训步骤和要求】
- 实现下图所示的用户界面设计。请参考讲义步骤写出程序编写步骤,并列出相应的源代码。
源项目:
array.xml源程序:
<resources>
<string-array name="user">
<item>0~10</item>
<item>10~20</item>
<item>20~40</item>
<item>40以上</item>
</string-array>
</resources>
activity.xml源程序:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent" //表示该布局的宽度与父容器(屏幕)相同
android:layout_height="wrap_content" > //表示该布局的高度恰好能包裹它的内容
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/etUserName"
android:layout_width="0dp" //编辑框与文本框的间距为0
android:layout_height="wrap_content"
android:layout_weight="1.0" //权重占1
android:hint="请输入用户名" />
</LinearLayout>
//文本框不需要id,逻辑框需要id
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" />
<EditText
android:id="@+id/etUserPass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:hint="请输入密码"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:" />
<EditText
android:id="@+id/etRealName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:hint="请输入真实姓名" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别/" />
<RadioGroup //单选栏需id
android:id="@+id/rgSex"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:checkedButton="@+id/rdoMale" //表示默认选中的按钮是”男”
android:orientation="horizontal" > //表示该布局从左到右排序
<RadioButton
android:id="@+id/rdoMale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="男" >
</RadioButton>
<RadioButton
android:id="@+id/rdoFemale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="女" >
</RadioButton>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="年龄:" />
<Spinner //Spinner为下拉列表,需要id
android:id="@+id/user"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:entries="@array/user"/> //指定列表的项数组
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox //复选框,需要id
android:id="@+id/chkRead"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="看小说" />
<CheckBox
android:id="@+id/chkPlayGame"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="玩游戏" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/chkEat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="美食" />
<CheckBox
android:id="@+id/chkSleep"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="睡觉" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/chkLookFilm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="看电影" />
<CheckBox
android:id="@+id/chkMunic"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="听音乐" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnRegister"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:onClick="doClick"
//点击按钮时会利用反射的方式调用对应Activity中的click()方法
android:text="注册" />
<Button
android:id="@+id/btnCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:onClick="doClick"
android:text="取消" />
</LinearLayout>
</LinearLayout>
项目成果图:
附注:该专栏是博主上学时的实训项目,可供访客练习与参考。代码质量不是很好,但能实现,仅供参考!