1、实验二 android基本控件实验 ———————————————————————————————— 作者: ———————————————————————————————— 日期: 15 个人收集整理 勿做商业用途 实验二 andro
2、id基本控件实验 【目的】 熟悉Android常用控件的基本操作,掌握它们的基本使用方法。了解控件之间的构成关系,熟悉适配器的使用原理。 【要求】 1、 了解控件的继承关系; 2、 掌握适配器的使用; 3、 掌握信息提示的实现机制; 4、 实现基本界面. 【原理】 1. 控件类之间的关系 android。view。View类(视图类)呈现了最基本的UI构造块。View类是Android中的一个超类,几乎包含了所有的屏幕类型,主要负责绘制和事件处理。 Android中控件类的扩展结构如图所示。 View有众多的扩展者,它们大部分是在android.widget包中,这
3、些继承者实际上就是Android系统中的“控件”.View实际上就是各个控件的基类,创建交互式的图形用户界面的基础。 View的直接继承者包括文本视图(TextView)、图像视图(ImageView)、进度条(ProgressBar)等。它们各自又有众多的继承者.每个控件除了继承父类功能之外,一般还具有自己的公有方法、保护方法、XML属性等. 在Android中使用各种控件的一般情况是在布局文件中可以实现UI的外观,然后在Java文件中实现对各种控件的控制动作。控件类的名称也是它们在布局文件XML中使用的标签名称。 2. 控件通用行为和属性 View是Android中所有控件类的基类
4、因此View中一些内容是所有控件类都具有的通用行为和属性。 提示:由于Java语言不支持多重继承,因此Android控件不可能以基本功能的“排列组合”的方式实现.在这种情况下,为了实现功能的复用,基类的功能往往做得较强,作为控件的祖先类,View所实现的功能也是最多的。 控件类经常在布局文件中使用,因此其可以使用XML属性(XMLAttributes),和Java代码经常具有对应关系. View作为各种控件的基类,其XML属性所有控件通用,XML属性及其对应的方法如表1所示。 表1 View中的XML属性及其对应的方法 XML属性名称 Java中的方法 描述 androi
5、d:background setBackgroundResource(int) 设置背景 android:clickable setClickable(boolean) 设置View是否响应单击事件 android:visibility setVisibility(int) 控制View的可见性 android:focusable setFocusable(boolean) 控制View是否可以获取焦点 android:id setId(int) 为View设置标识符,可通过findViewById方法获取 android:longClickable setLo
6、ngClickable(boolean) 设置View是否响应长单击事件 android:soundEffectsEnabled setSoundEffectsEnabled(boolean) 设置当View触发单击等事件时是否播放音效 android:saveEnabled setSaveEnabled(boolean) 如果未作设置,当View被冻结时将不会保存其状态 android:nextFocusDown setNextFocusDownId(int) 定义当向下搜索时应该获取焦点的View,如果该View不存在或不可见,则会抛出RuntimeException异
7、常 android:nextFocusLeft setNextFocusLeftId(int) 定义当向左搜索时应该获取焦点的View android:nextFocusRight setNextFocusRightId(int) 定义当向右搜索时应该获取焦点的View android:nextFocusUp setNextFocusUpId(int) 定义当向上搜索时应该获取焦点的View 其中,android:id表示控件的标识,通常需要在布局文件中指定这个属性.View中与控件标识相关的几个方法如下所示: public int getId()
8、 // 获得控件的id(int类型) public void setId(int id) // 设置控件的id(int类型) public Object getTag() // 获得控件的tag(Object类型) public void setTag(Object tag) // 设置控件的tag(Object类型) 对于一个控件,也就是View的继承者,整数类型id是其主要的标识。其中,getId()可以获得控件的id,而setId()可以将一个整数设置为控件的id,但是这个方法并不
9、常用。View的id通常可以在布局文件中获得。 Object类型的标识tag是控件的一个扩展标识,由于使用了Object类型,它可以接受大部分的Java类型。 在一个View中根据id或者tag查找其孩子的方法如下所示: public final View findViewById(int id) public final View findViewWithTag(Object tag) findViewById()和findViewWithTag()的目的是返回这个View树中id和tag为某个数值的View的句柄.View树的含义是View及其所有的孩子. 值得注意
10、的是,id不是控件的唯一标识,例如布局文件中id是可以重复的,在这种重复的情况下,findViewById()的结果不能确保找到唯一的控件。 提示:作为控件的标识的id和tag可以配合使用:当id有重复的时候,可以通过给控件设置不同的tag,对其进行区分. 可见性的问题,android:visibility在布局文件中有三个数值:visible(可见,默认),invisible(不可见),gone(去除)。在Java代码中,setVisibility()能使用的枚举值与其对应,它们是:View.VISIBLE(0x0),View.INVISIBLE(0x4),View.GONE(0x8)
11、 3。 数据适配器工作原理 Adapter是视图与数据之间的桥梁,Adapter用来帮助填充视图中的数据,提供视图对数据的访问方式,通过它将数据填充到控件中,为在数据集里的每个数据项生成一个View。 常用Adapter包括ArrayAdapter, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdapter。 各个Ada
12、pterView之间的继承关系如图: 基类BaseAdapter有一个重要的方法: public abstract View getView (int position,View convertView,ViewGroup parent) 参数 Int position :该视图在适配器数据集中的位置 View convertView :旧视图,也就已经滚出可视区的视图 ViewGroup parent :当前视图最终会被附加到的父级视图 这个方法被setAdapter(adapter)间接地调用.getView 方法的作用是得到一个View,这个view显示数据项里指定位置的
13、数据,可以手动创建一个view或者从一个XML layout中解析获取。当这个view被inflated,它的父view(如GridView,ListView等)将要使用默认的layout参数,除非你用inflate(int,android.view。ViewGroup,boolean)方法来指定一个根view并防止附着在根上。在Adapter中,所有数据项的视图是可以重用的,只是对可见视图将产生视图,对已经滚动出可见界面的视图,将重复使用,以减少资源损耗。 【过程】 1. 新建一个Android工程ControlTest; 2. 编写布局文件,实现布局效果,如图: 【源代码】
14、1、 主界面布局文件main。xml
<?xml version=”1。0" encoding=”utf—8”?>
〈LinearLayout xmlns:android=”http://schemas.android。com/apk/res/android”
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation=”vertical”
android:background="#ffffff" 〉
15、yout
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:orientation="horizontal"〉”
16、Top=”10dp”
android:paddingLeft="10dp”
android:text=”姓名:”
android:textColor="#000000”
android:textSize="15sp"/>
17、id/editxt_name"
android:layout_width=”200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"/〉
18、droid:orientation="horizontal”〉
19、olor="#000000"
android:textSize="15sp”/〉
〈EditText
android:background="@drawable/edit_text"
android:textSize=”15sp”
android:id=”@+id/editxt_id"
android:layout_width=”200dp”
android:layout_height=" 20、wrap_content"
android:layout_marginLeft="3dp"/>
〈/LinearLayout〉
21、th=”wrap_content"
android:layout_height=”wrap_content"
android:paddingTop="10dp"
android:paddingLeft=”10dp”
android:text=”班级:”
android:textColor=”#000000”
android:textSize="15sp"/>
〈EditText
android 22、background=”@drawable/edit_text”
android:textSize=”15sp"
android:id="@+id/editxt_class"
android:layout_width=”200dp”
android:layout_height="wrap_content”
android:layout_marginLeft=”3dp"/>
〈/LinearLayout〉
〈LinearL 23、ayout
android:layout_width="fill_parent”
android:layout_height=”wrap_content”
android:orientation=”horizontal”〉
24、op="10dp"
android:paddingLeft="10dp”
android:text="年级:"
android:textColor=”#000000”
android:textSize=”15sp"/>
25、"@+id/editxt_grade"
android:layout_width="200dp”
android:layout_height="wrap_content"
android:layout_marginLeft=”3dp"/>
26、t"
android:orientation="horizontal">"
〈TextView
android:id="@+id/spinner_view”
android:layout_width="wrap_content”
android:layout_height="wrap_content”
android:text="课程:"
android:textSize="15sp"
android:paddingTop=”10dp”
27、 android:paddingLeft=”10dp"
android:textColor=”#000000”
/〉
〈Spinner
android:id="@+id/course_spinner"
android:layout_width=”wrap_content”
android:layout_height="wrap_content"
android:background="@drawable/spinner_default_holo_light"
28、
/〉
29、e="15sp”
android:textColor="#ffffff"
android:onClick="OnAddClick”
android:background="#7b7979”/〉"
30、parent"
android:background="#ffffff"
/〉
〈LinearLayout xmlns:android=”http://schemas。
android:layout_width="fill_parent"
android:layout_height="match_parent”
android: 31、orientation=”vertical”
android:background="#ffffff”〉
〈LinearLayout
android:orientation=”horizontal"
android:layout_width=”fill_parent”
android:layout_height="wrap_content"
>
〈TextView
android:id="@+id/item_name"
and 32、roid:layout_width="0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1"
android:textColor="#000000"
android:textSize=”10sp" /〉
〈TextView
android:id="@+id/item_num"
android:layou 33、t_width=”0dp"
android:layout_height="wrap_content”
android:layout_weight=”1”
android:textColor="#000000”
android:textSize="10sp" /〉
〈TextView
android:layout_width="0dp"
androi 34、d:layout_height=”wrap_content"
android:layout_weight="1”
android:id=”@+id/item_class”
android:textSize=”10sp"
android:textColor=”#000000”
/>
〈TextView
android:layout_width="0dp”
an 35、droid:layout_height="wrap_content”
android:layout_weight=”1"
android:id=”@+id/item_course"
android:textSize="10sp"
android:textColor=”#000000”
/〉
〈Button
android:layout_width=”0dp"
36、android:layout_height=”wrap_content”
android:layout_weight=”1"
android:id="@+id/item_del"
android:textSize="20sp"
android:textColor="#000000”
/>
"
〈/LinearLayout〉”
37、on=”1。0” encoding="utf-8”?>
〈resources〉
〈string-array name="course_list”>
〈item 〉语文数学〈/item〉
38、xml version=”1。0" encoding="utf—8”?〉
39、式的定义:
〈?xml version="1.0" encoding="utf—8"?>
40、6、 添加按钮的事件定义:
public void OnAddClick(View v)//添加按钮事件
{
//将所输入的信息转换为字符串
String name=mTextName.getText()。toString();
String id=mTextId。getText()。toString();
String classes=mSpinnerCourse。getSelectedItem().toString();
String grade=mTextGrade.getText().toString() 41、
//将所输入的信息添加到mDataList中
mDataList。add(new Item(id,name,grade,classes));
//刷新listview
mListAdapter。notifyDataSetChanged();
//创建toast提示添加成功
toast=Toast。makeText(getApplicationContext(),
”添加成功!”, Toast.LENGTH_LONG);
toast.setGravity(Gravit 42、y.CENTER, 0, 0);
toast.show();
}
7、 删除按钮的事件定义:
mViewHolder.mDelButton//删除按钮事件
。setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mArrayitem。remove(id — 1);
ItemAdapter.this.notifyDataSetCha 43、nged();
//创建toast提示删除成功
toast=Toast。makeText(mContext,
”删除成功!", Toast.LENGTH_LONG);
toast。setGravity(Gravity。CENTER, 0, 0);
toast。show();
}
});
8、 Item类的定义:
package com。example.classview;
public class Item {
public Str 44、ing course;
public String id;
public String grade;
public String name;
public Item(String id, String name, String grade,String course) {
super();
this。id = id;
this.name = name;
this.grade = grade;
this。course = course;
}
}
五、实验结果及分析
输入相应的信息后,点击添加按钮,界面如下图:
单击删除按钮后界面显示为:
六、 实验心得
本次实验目的是熟悉Android常用控件的基本操作,掌握它们的基本使用方法.了解控件之间的构成关系,熟悉适配器的使用原理。刚拿到实验时无从下手,后来通过网络学习和向人请教得以解决,实验后对基本控件的使用也有了基本的了解,对某些控件的掌握度不是很高,通过对相关知识的强化理解,基本掌握该控件的使用方法;对信息提示机制还不够熟悉,实验进行的有点困难,仍需通过强化相关知识,动手实践,熟悉相关内容;本次实验相对来说,略有难度。






