资源描述
实验二Android基本控件应用
【实验目的】
1、掌握Android下常用控件的使用方法
2、了解Android控件分类
【实验要求】
1、学习Android下控件使用
2、学习Android中添加控件
【实验原理】
控件是为用户交互界面提供服务的视图对象。Android提供了一套完整的控 件,包括按钮、复选框、文本输入框、菜单、对话框等等,以助于快速地构建 UL Android还提供了一些更高级的控件,如日期选择、时钟以及缩放控制。
Android常用的控件:
1、文本控件(Textview 和 EditText)
在Android中文本控件分为:TextView控件和EditText控件TextView 控件
TextView控件继承自View类。TextView控件的功能是向用户显示文本 内容,同时可选择性让用户编辑文本。其中TextView不允许编辑,但其子类允 许编辑。
EditText 控件
EditText控件继承自TextViewo EditText与TextView最大的不同是 EditText是可以编辑的。
2、按钮控件(Button 和 ImageButton)
Android中的按钮可以分为:Button和ImageButton两种控件Button控件
Button控件继承自TextView类,Button的用法比拟简单,主要是为 Button控件设置View.OnClickListener.监听器并在监听器的实现代码中开发 按钮按下事件的处理代码。
ImageButton 控件
ImageButton 继承自 ImageViewo ImageButton 与 Button 最大的区别是 ImageButton没有text属性,既按钮中将显示图片而不是文本。
ImageButton控件中设置显示图片可以通过androicksrc属性,也可以通过 setlmageResurce(int)方法来实现。
3、状态开关按钮(ToggleButton)
ToggleButton控件是继承自Buttono ToggleButton的状态只能是选中和 为选中,并且需要为不同的状态设置不同的显示文本。除了继承自父类的一些属 性和方法之外,ToggleButton也具有一些自己的ToggleButton属性。
4、单项选择按钮和复选按钮(CheckBox RadioButton)
CheckBox和RadioButton都只有选中和为选中两种状态,可以通过 android: check属性来设置。不同的是RadioButton是单项选择按钮,需要编制到 一个RadioGroup中,同一时刻一个RadioGroup中只能有一个按钮处于选中 状态。
5、图片控件(ImageView)
ImageView控件负责显示图片,其图片来源既可以是资源文件的id,也可 以是Drawable对象或Bitmap对象,还可以是Content Provider的Uri。
6、菜单(Menu)
菜单的设计在人机交互中可以说是非常人性化的,它提供了不同功能分组展 示的能力。Android中菜单分为三种类型:选项菜单(Option Menu)、上下文菜单 (Context Menu)和子菜单(Sub Menu )。
7、对话框(Diaiog)
对话框是程序运行中的弹出窗口。例如,当用户要删除一个联系方式时,会 弹出一个对话框,让用户确认是否真的要删除。Android系统提供了四种对话框: 警告对话框(AlertDialog )、进度对话框(ProgressDiaiog)、日期选择对话框 (DatePickerDialog)和时间选择对话框(TimePickerDialog)。
【实验步骤】下面是一个基本控件的实例
1>创立一个工程,命名为ex04_widgets2、界面main.xml的代码如下:
<?xml version="1.0" encoding=,,UTF-8',?><LinearLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="verticar, xmlns:android=""> <TextView
android:layout_height="wrap_content"android:layout_width ="fill_parent” android :text=,,@string/hello"/>
<!-建立一个单项选择项组<RadioGroup
android :layout_height="wrap_content" android:layout_width="wrap_content" androickorientation ="horizontal” android:id="@+id/radioGroupl"><!-添加三个单项选择控件
<RadioButtonandroid :layout_height="wrap_content"
android :layout_width=,,wrap_content"android :text="@string/big"
android:id="@+id/radioO"android:checked="true"/>
<RadioButtonandroid :layout_height=Mwrap_content"
android :layout_width="wrap__content"android:text="(5)string/mid"
android:id="@+id/radiol,7><RadioButton
android:layout_height="wrap_content"android :layout_width="wrap__content"
android :text=n@string/smaH"android:id="@+id/radio2'7>
</RadioGroup><TextView
android :layout__height="wrap_content"android:layout_width="wrap_content"
androicktext="多项选择:”android:id="(5)+id/textViewl"
android :layout_marginTop="10px,7><!-创立多项选择组->
<TableLayoutandroid:layout_height="wrap_content"
android :layout_width="filLparent"android:id=,,@+id/tableLayoutl">
<TableRow android:layout_height="wrap_content"android :layout_width="wrap__content"
android:id="@+id/tableRow2"><CheckBox android:layout_height="wrap_content"
android:layout_width="wrap_content"android:text="A"
android :id="@+id/checkBox3,7><CheckBox
android:layout_height="wrap_content"android:layout_width=,,wrap_content"
android:text="B"android :id="@+id/checkBox9"
android:layout_marginLeft="50px,7></TableRow>
<TableRow android:layout_height=,,wrap_content"android:layout_width="wrap_content" android:id="@+id/tableRow3">
<CheckBoxandroid :layout_height="wrap_content"
android:layout_width=',wrap_content"android:text="C"
android :id="@+id/checkBox4,7><CheckBox
android:layout_height="wrap_content"
android :layout_width="wrap_content" android:text="D"
android:id="(5)+id/checkBox5"android:layout_marginLeft="50px,,/>
</TableRow></TableLayout>
<TextViewandroid :layout_height="wrap_content"
android:layout_width="fill_parent"androicktext 二"颜色选择”
android :layout_marginTop=,,10px"/><Spinner
android :layout_height="wrap_content"android:layout_width="fill_parent"
android:id="(5)+id/spinnerl"android :entries=,,@array/colors"
android :prompt="@string/spinnerTitle"></Spinner>
<Buttonandroid:layout_height=,,wrap_content"
android:layout_width=,,wrap_content,,android :text=,,@string/submit,7>
</LinearLayout>3、arrays.xml中的代码如下
<?xml version=,,1.0" encoding="UTF-8"?><resources>
<string-array name="colors"><item>red</item>
<item>orange</item><item>yellow</item>
<item>green</item><item>blue</item>
</string-array></resources>
Strings.xml代码如下<?xml version=,,1.0" encoding="UTF-8"?>
<resources><string name="hello”〉单项选择:</string>
<string name="app_name">exO4_Widgets</string><string name="big”>大</string〉
<string name="mid">44</string><string name=,,smaH">/J^</string>
<string name="spirmerTitle”>请选择颜色〈/string〉<string name="submit"〉确认〈/string〉
</resources>4、创立Widgets.java,其中的代码如下
package com.widgets;import android.app.Activity;
import android.os.Bundle;public class Widgets extends Activity {
/** Called when the activity is first created. */@Override
public void onCreate(Bundle savedlnstanceState) { super.onCreate(saved Instancestate);setContentView(R.layout.main);
})
【实验结果】运行效果图如图2.1所示:
Widgets 单项选择:
大中小
多项选择:
■ a・b■ cBd
颜色选择
图2.1基本控件应用示意图
展开阅读全文