- 浏览: 5479 次
- 性别:
- 来自: 西安
文章分类
最新评论
Android学习笔记
一、 Android环境
1.环境简介
2.环境搭建
3.Android项目结构说明
二、用户界面设计
1.UI界面控制
简介:
用户界面设计时Android应用开发的一个重要内容。在进行用户界面设计时,首先要了解页面中的UI元素如何呈现给用户,也就是如何控制UI界面,Android提供了4种控制UI界面的方法
方法分类:
1. 使用XML布局文件控制UI界面
Android提供了一种非常简单、方便的方法用于控制UI界面。该方法采用XML文件来进行界面布局,从而将布局界面的代码和逻辑控制的java代码分离开来,使程序的结构更加清晰、明了。
具体步骤:
(1) 在Android应用的res/layout目录下面编写XML布局文件,可以采用任何符合java命名规则的文件名,创建后R.java会自动收录布局资源的。
(2) 在Activity中使用以下Java代码显示XML文件中布局的内容
setContentView(R.layout.main)
注:main是XML布局文件的文件名
通过上面两个步骤就可以轻松实现布局并显示UI界面的功能。
具体事例:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:backgourd="@drawable/background">
<!-- 添加提示文字 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/title"
style="@style/text"/>
<!-- 添加开始按钮 -->
<TextView
android:id="@+id/startButton"
android:layout_gravity="center_horizontal|center_vertical"
android:text="@string/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/text"/>
</FrameLayout>
此布局管理器采用的是帧布局(FrameLayout)并且添加了两个TextView组件,第一个用于显示文件,第2个用于在窗体的正中间位置显示开始游戏按钮
在布局管理文件main.xml中通过设置布局管理器的android:backgourd属性可以为窗体设置背景图片,通过设置具体组件的style,可以为组件设置样式,使用android:layout_gravity="center_horizontal|center_vertical"可以让该组件在帧布局中居中显示
修改res/values目录下的string.xml文件,并在该文件中添加一个用于定义开始按钮内容的常量,名为start,内容为“单机开始游戏…”如下图:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, XMLLayoutActivity!</string>
<string name="app_name">1.2</string>
<string name="title">使用XML布局文件控制UI界面</string>
<string name="start">单机开始游戏...</string>
</resources>
string.xml文件用于定义应用程序中的字符串常量。其中,meiyige<string>子元素都可以定义一个字符串常量,常量名称有name属性指定,常量内容写在起始标签和结束标签之间
为了改变窗口中文字的大小,需要为TextView组件添加style样式属性,用于指定应用的样式,具体的样式需要在res/values目录中创建的样式文件中指定,如创建一个名为styles.xml的样式文件,并在该文件中创建一个名称为text的样式,用于指定文字的大小和颜色,具体内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="text">
<item name="android:textSize">24px</item>
<item name="android:textColor">#f8f8f8</item>
</style>
</resources>
在主活动(MainActivity)中,应用以下代码,指定活动应用的布局文件:
setContentView(R.layout.main);
具体运行结果如下:
2. 在代码中控制UI界面
简介:
Android支持像Java Swing那样完全通过代码控制UI界面,也就是所有的UI界面通过new关键字创建出来,然后将UI组件添加到布局管理器中,从而实现用户界面
具体步骤:
(1) 创建布局管理器,可以使帧布局、表格布局、线性布局和相对布局,并设置布局管理器的属性
(2) 创建具体组件,可以使TextView、ImageView、EditText和Button等Android提供的组件,并设置组件的布局和各种属性
(3) 将创建的组件添加到布局管理其中
特点:
完全通过代码控制UI界面虽然比较灵活,但是其开发过程比较繁琐,而且不利于高层次的解耦,因此不推荐采用这种方式控制UI界面。
具体事例:
package com.caillc;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;
public class JavaLayoutActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
// 创建布局管理器
FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
// 创建TextView组件
TextView tv = new TextView(this);
tv.setText("在代码中控制UI界面");
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
tv.setTextColor(Color.rgb(25, 255, 25));
// 添加组件到布局管理器
frameLayout.addView(tv);
// 创建按钮组件
TextView tw = new TextView(this);
tw.setText("点击进入游戏...");
tw.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
tw.setTextColor(Color.rgb(25, 255, 25));
// 创建一个布局参数
LayoutParams params = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
// 设置布局参数
tw.setLayoutParams(params);
// 给组件添加时间监听器
tw.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 设置对话框
new AlertDialog.Builder(JavaLayoutActivity.this)
.setTitle("系统提示")
.setMessage("游戏有风险,进入需谨慎,真的要进入吗?")
// 设置对话框的内容
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log.i("3.1.2", "进入游戏");// 输入日志
}
})
.setNegativeButton("退出",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log.i("3.1.2", "退出游戏");
finish(); // 结束游戏
}
}).show(); // 显示对话框
}
});
//添加组件
frameLayout.addView(tw);
}
}
代码中有注释可以自己看
显示效果:
点击按钮:
点击“确定”对话框消失,点击退出直接退出应用
3. 使用XML和Java代码混合控制UI界面
简介:
完全通过XML布局文件控制UI界面,实现比较方便快捷,但是有失灵活;而完全采用java代码控制UI界面,虽然比较灵活,但是开发过程比较繁琐,鉴于两种方法的优缺点,可以融合两种方式,同时采用两种方式进行UI界面的控制。
使用XML和java控制UI界面,习惯上把变化小,行为比较固定的组件放在XML布局文件中,把变化较多,行为控制比较复杂的组件交给Java代码来管理
具体事例:
第一步修改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="horizontal"
android:background="@drawable/background"
android:id="@+id/layout">
</LinearLayout>
第二步在主活动中添加组件:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class JavaAndXMLActivity extends Activity {
private ImageView[] img = new ImageView[4];
private int[] imagePath = new int[] {
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)this.findViewById(R.id.layout);
for(int i=0; i<imagePath.length; i++) {
img[i] = new ImageView(this);
img[i].setImageResource(imagePath[i]);
img[i].setPadding(2, 2, 2, 2);
LayoutParams params = new LayoutParams(253,148);
img[i].setLayoutParams(params);
layout.addView(img[i]);
}
}
}
运行效果如下:
4. 开发自定义的View
简介:
在Android中所有的UI界面都是由View类和ViewGroup类及子类组合而成的,其中View是所有类的基类,而ViewGroup类是容纳这些UI组件的容器,其本身也是View类的子类。在ViewGroup类中,除了可以包含普通的View类外,还可以再次包含ViewGroup类。
操作步骤:
(1) 创建一个继承android.view.View类的View类,并且重写构造方法。
(2) 根据需要,重写相应的方法。
(3) 在项目的活动中,创建并实例化自定义的View类,将其添加到布局管理器中。
具体事例(无)
3.SDK中常用命令
2布局管理器
2.1简介
每个组件在窗体中都有具体的位置和大小,在窗体中摆放各种组件时很难进行判断。使用Android布局管理器可以很方便的控制各组件的位置和大小。
Android中提供了“线性布局管理器(LinearLayout)”、“表格布局管理器(TableLayout)”、“帧布局管理器(FrameLayout)”、“相对布局管理器(RelativeLayout)”和“绝对布局管理器(AbsoluteLayout)”。其中最后一种绝对布局管理器在Android2.0中被标记为已过期,可以使用帧布局或相对布局替代。
2.2线性布局
简介:
“线性布局管理器”是将放入其中的组件垂直或水平方向来布局,也就是控制放入其中组件横向或纵向排列。
特点:
在线性布局中,每一行(针对垂直排列)或每一列(针对水平排列)中,只能放一个组件,并且Android的线性布局不会换行,当组件排列到窗口边缘后,后面的组件将不会被显示出来。
使用说明:
1. 使用XML布局文件定义线性布局管理器
代码实例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:id="@+id/layout">
</LinearLayout>
代码分解说明:
XML属性 描述
linearLayout
在XML布局管理器中定义线性管理器时,需要使用<linearLayout>标记,其基本的语法格式如下:
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
属性列表
>
</LinearLayout>
android:orientation 用于设置布局管理器内组件的排列方式,其可选值有两种:horizontal:表示水平排列;
vertical:表示垂直排列;
android:gravity 用于设置布局管理器内组件的对齐方式,其可选值包括:
top: 靠上对齐
bottom: 靠下对齐
left: 靠左对齐
right: 靠右对齐
center_vertical: 垂直居中
fill_vertical:增加组件的垂直尺寸以在垂直方向填满容器。
center_horizontal: 水平居中
fill_horizontal: 增加组件的水平尺寸以在水平方向填满容器。
center: 垂直和水平方式都居中,不改变组件大小
fill:水平和垂直方向充满容器
clip_vertical:将组件的垂直方向的上下边向容器靠拢
clip_horizongtal: 将组件的水平方向的左右边向容器靠拢
这些属性也可以同时指定,各属性之间用竖线“|”隔开,如:
right|bottom:指定组件靠右下角对齐
android:
layout_width 设置组件的基本宽度,其值可选如下:
fill_parent:表示该组件的宽度与容器的宽度相同;
match_parent:与fill_parent的作用完全相同,从Android2.2开始推荐使用;
wrap_content:表示该组件的宽度恰好能包住组件的内容;
android:
layout_height 设置组件的基本高度,其值可选如下:
fill_parent:表示该组件的高度与容器的宽度相同;
match_parent:与fill_parent的作用完全相同,从Android2.2开始推荐使用;
wrap_content:表示该组件的高度恰好能包住组件的内容;
android:id 用于为当前组件指定一个id属性;在java代码中可以单独引用这个组件。为组件指定id后,在R.java文件中,会自动派生一个对应的属性,在java代码中,可以通过findViewById()方法来获取它
android:
backgroud 为组件设置背景,可以是背景图片,也可以是背景颜色,为组件指定背景图片时,可以将准备好的背景图片复制到目录res/drawable-hdpi或
res/drawable-ldpi或
res/drawable-mdpi下,然后使用下面代码进行设置引用:
android:background="@drawable/background"其中@drawable指的是上面的目录,而background则表示图片的名称。
如果想指定背景颜色,可以使用颜色值如:
android:background="#ffffff"
备注: 1.属性android:orientation和android:gravity是线性布局管理器支持的属性,其他几个属性都是android.view.View和android.view.ViewGroup支持的属性
2.属性android:layout_width和android:layout_height是ViewGroup.LayoutParams所支持的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="horizontal"
android:background="@drawable/background"
android:id="@+id/layout" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="按钮1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="按钮2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="按钮3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="按钮4" />
</LinearLayout>
效果:
代码:
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮4" />
</LinearLayout>
效果:
2. 使用Java代码来创建(不建议使用)
2.3表格布局
简介:
表格布局与常见的表格布局类似,以行、列的形式来管理放入其中的UI组件。表格布局适用<TableLayout>标记定义,在表格布局中,可以添加多个<TableRow>标记,每一个<TableRow>标记占用一行,其中<TableRow>也是容器,所以还可以在它其中添加其他组件,每添加一个组件,表格就会增加一列。
特点:
在表格布局中,列可以别隐藏,也可以被设置为伸展的,从而填充可利用的屏幕空间,还可以设置为强制收缩,直到匹配屏幕大小
如果在表格布局中,直接向<TableLayout>中添加UI组件,那么该组件将独占一行。
表格布局管理器,可以使用XML布局文件中定义的表格布局管理器,也可以使用Java代码创建,不过推荐前者。
TableLayout继承了LinearLayout,因此,它完全支持LinearLayout所支持的全部的XML属性,此外它还有自己特有的XML属性。
使用说明:
代码实例:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3E3C89"
android:gravity="center_vertical"
android:stretchColumns="0,3">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView />
<TextView android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24px"
android:id="@+id/textView1"/>
<EditText android:id="@+id/editText1"
android:textSize="24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200px"/>
<TextView />
</TableRow>
</TableLayout>
特色属性说明
XML属性 描述
android:stretchColumns 设置允许被拉伸的列的列号(序号从0开始),多个序号之间用逗号“,”分割
android:collapseColumns 设置允许被隐藏的列的列号(序号从0开始),多个序号之间用逗号“,”分割
android:shrinkColumns 设置允许被收缩的列的列号(序号从0开始),多个序号之间用逗号“,”分割
具体事例:
实例代码:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3E3C89"
android:gravity="center_vertical"
android:stretchColumns="0,3">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView />
<TextView android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24px"
android:id="@+id/textView1"/>
<EditText android:id="@+id/editText1"
android:textSize="24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200px"/>
<TextView />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView />
<TextView android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24px"
android:id="@+id/textView2"/>
<EditText android:id="@+id/editText2"
android:textSize="24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200px"
android:inputType="textPassword" />
<TextView />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView />
<Button android:text="登录"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:text="登录"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView />
</TableRow>
</TableLayout>
实例效果:
2.4帧布局
简介:
在帧布局管理器中,每加入一个组件,都降创建一个空白的区域,通常称为一帧,这些帧都会根据gravity属性执行自动对齐,默认情况下,帧布局从屏幕的左上角(0,0)坐标点开始布局,多个组件层叠排序,后面的组件覆盖前面的组件。
可以通过XML和java两种方式定义帧布局管理器,我们推荐前者
属性:
XML属性 描述
android:foreground 设置该帧布局容器的前景图像
android:foregroundGravity 定义绘制前景图像的gravity属性,即前景图像显示的位置。
备注:
帧布局管理器经常应用在游戏开发中,用于显示自定义的视图
具体事例:
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:foreground="@drawable/ic_launcher"
android:foregroundGravity="bottom|right">
<!-- 添加居中显示的红色背景的TextView,将显示在下面 -->
<TextView android:text="红色背景的TextView"
android:id="@+id/textView1"
android:background="#ffff0000"
android:layout_gravity="center"
android:layout_width="400px"
android:layout_height="400px"/>
<!-- 添加居中显示的橙色背景的TextView,将显示在下面 -->
<TextView android:text="红色背景的TextView"
android:id="@+id/textView1"
android:background="#ffff6600"
android:layout_gravity="center"
android:layout_width="300px"
android:layout_height="300px"/>
<!-- 添加居中显示的黄色背景的TextView,将显示在下面 -->
<TextView android:text="红色背景的TextView"
android:id="@+id/textView1"
android:background="#ffffee00"
android:layout_gravity="center"
android:layout_width="200px"
android:layout_height="200px"/>
</FrameLayout>
显示效果:
2.5 相对布局
简介:
相对布局是指按照组件之间的相对位置来进行布局,如某一个组件在另外一个组件的左边、右边、上边或者下边。相对布局使用标签<RelativeLayout>
使用相对布局有两种方式:XML和Java编码,我们推荐使用前者.
属性:
XML属性 描述
android:gravity 用于设置布局管理器中各组件的对齐方式
Android:ignoreGravity 用于指定哪个组件不受gravity属性的影响
在相对布局管理器中,只有上面介绍的两个属性是不够的,为了更好的控制布局管理器中各子组件的布局分布,RelativeLayout提供了一个内部类RelativeLayout.LayParams,通过该类提供大量的XML属性,可以很好的控制相对管理器中各组件的分布方式。其具体属性如下
XML属性 描述
android:layout_above 其属性值为其他UI组件的id属性,用于指定该组件位于哪个组件的上方
android:layout_below 其属性值为其他UI组件的id属性,用于指定该组件位于哪个组件的下方
android:layout_toLeftOf 其属性值为其他UI组件的id属性,用于指定该组件位于哪个组件的左侧
android:layout_toRightOf 其属性值为其他UI组件的id属性,用于指定该组件位于哪个组件的右侧
android: layout_alignTop 其属性值为其他UI组件的id属性,用于指定该组件与哪个组件的上边界对齐
android: layout_alignBottom 其属性值为其他UI组件的id属性,用于指定该组件与哪个组件的下边界对齐
android: layout_alignLeft 其属性值为其他UI组件的id属性,用于指定该组件与哪个组件的左边界对齐
android: layout_alignRight 其属性值为其他UI组件的id属性,用于指定该组件与哪个组件的右边界对齐
android:layout_alignParentTop 其属性值为boolean值,用于指定该组件是否与布局管理器的顶端对齐
android:layout_alignParentBottom 其属性值为boolean值,用于指定该组件是否与布局管理器的底端对齐
android:layout_alignParentLeft 其属性值为boolean值,用于指定该组件是否与布局管理器的左边对齐
android:layout_alignParentRight 其属性值为boolean值,用于指定该组件是否与布局管理器的右边对齐
android: layout_centerHorizontal 其属性值为boolean值,用于指定该组件是否位于布局管理器水平居中的位置
android: layout_centerVertical 其属性值为boolean值,用于指定该组件是否位于布局管理器垂直居中的位置
android: layout_centerInParent 其属性值为boolean值,用于指定该组件是否位于布局管理器中央位置
具体事例:
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/relativeLayout1"
android:background="@drawable/background">
<!-- 添加一个居中显示的文本视图 -->
<TextView android:text="发现有Widget的新版本,您想现在就安装吗?"
android:id="@+id/textView1"
android:textSize="24px"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<!-- 添加一个在button2左侧显示的按钮button1 -->
<Button android:id="@+id/button1"
android:text="现在更新"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toLeftOf="@+id/button2"/>
<!-- 添加一个在button2 该按钮与textView1的右边界对齐 -->
<Button android:id="@+id/button2"
android:text="现在更新"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_alignRight="@+id/textView1"/>
</RelativeLayout>
显示效果:
3.基本组件
3.1文本框和编辑框
简介:
Android提供了两种文本组件:一种是文本框(TextView),用于屏幕上显示文本,相当于Java中的标签,也就是JLable;另一种是编辑框(EditText)用于在屏幕上显示可编辑的文本;其中EditText是TextView的子类。
向屏幕添加文本框有两种方式,一种是XML,一种是通过Java方式new,我们推荐使用第一种。
1. 文本框
基本语法:
<TextView
属性列表
>
</TextView>
TextView常用的XML属性
android:autoLink 用于指定是否将指定格式的文本转换为可单击的超链接形式,其属性值有none、web、email、phone、map和all
android:drawableBottom 用于在文本框内文本的底端绘制指定图像、该图像可以是放在res\drawable目录下的图片,通过“@drawable/文件名(不包括文件扩展名)”设置
android:drawableLeft 用于在文本框内文本的左侧绘制指定图像、该图像可以是放在res\drawable目录下的图片,通过“@drawable/文件名(不包括文件扩展名)”设置
android: drawableRight 用于在文本框内文本的右侧绘制指定图像、该图像可以是放在res\drawable目录下的图片,通过“@drawable/文件名(不包括文件扩展名)”设置
android: drawableTop 用于在文本框内文本的上端绘制指定图像、该图像可以是放在res\drawable目录下的图片,通过“@drawable/文件名(不包括文件扩展名)”设置
android:gravity 用于设置文本框内文本的对齐方式,可选值有top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、和clip_horizontal等。这些属性值也可以同时指定,各属性值之间用竖线隔开。例如要指定组件内容靠右下角对齐,可以使用属性right|bottom.
android:hint 用于设置文本框为空时,默认显示的提示文本
android:inputType 用于指定当前文本框显示内容的文本类型,其可选值有textPassword、textEmailAddress、phone、date等,可以同时指定多个,使用“|”分隔
android:singleLine 用于指定该文本框是否为单行模式,其属性为有true和false,为true时表示文本框不会换行,当文本内容超过一行时,超过的部分将被省略,同时再结尾处添加“…”
android:text 用于指定文本框内显示的文本内容,可以直接在该属性值中指定,也可以通过string.xml文件中定义的文本常量的方式指定
android:textColor 用于设定文本内容的颜色,其属性值可以是#rgb、#argb、#rrggbb或#aarrggbb格式指定的颜色
android:textSize 用于指定文本文字的大小,其属性由代表大小的数值和单位组成,其单位可以有px、pt、sp和in等
android:width 用于指定文本框的宽度,单位为像素
android:height 用于指定文本框的高度,单位为像素
备注:上面的属性只是TextView组件的部分常用的属性,关于该组件的其他属性,可以参考Android官方提供的API文档。
代码实例:
<?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="@drawable/background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:autoLink="email"
android:height="50px"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上方带图片的TextView"
android:drawableTop="@drawable/icon"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左侧带图片的TextView"
android:drawableLeft="@drawable/icon"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下方带图片的TextView"
android:drawableBottom="@drawable/icon"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多行文字,1979年,那是一个春天啊,有一位老人在中国的南海边画了一个圈,生活变得不一样啊!"
android:width="300px"
android:textSize="20px"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多行文字,1979年,那是一个春天啊,有一位老人在中国的南海边画了一个圈,生活变得不一样啊!"
android:singleLine="true"
android:width="300px"
android:textSize="20px"/>
</LinearLayout>
效果展示:
2. 编辑框
Android中的编辑框可以输入单行文字,也可以输入多行文本,还可以输入指定格式的文本(如密码、电话号码、E-mail地址等)
基本语法:
<EditText
属性列表
>
</<EditText>
支持XML的属性:
由于EditText类是TextView的子类,所以针对TextView的所有的属性,EditText都可以使用,需要注意的是EditText组件中,android:inputType属性可以帮助输入框显示合适的类型。如输入的格式如果是密码,则其值可以是textPassword
获取编辑框的内容:
在屏幕中添加了编辑框后,还需要获取编辑框中的内容,这里可以通过编辑框组件提供的getText()方法实现。使用该方法时,先要获取到编辑框的组件,然后再调用getText()方法,如:获取布局文件中id为login的编辑框的内容,可以通过以下代码实现:
EditText login = (EditText)findViewById(R.id.login);
String loginText = login.getText().toString();
代码实例:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="50px"
android:text="会员昵称:" />
<EditText
android:id="@+id/nickName"
android:layout_height="wrap_content"
android:hint="请输入会员昵称"
android:singleLine="true"
android:width="300px" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="50px"
android:text="输入密码:" />
<EditText
android:id="@+id/pwd"
android:layout_height="wrap_content"
android:hint="请输入会员昵称"
android:inputType="textPassword"
android:singleLine="true"
android:width="300px" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="50px"
android:text="密码确认:" />
<EditText
android:id="@+id/pwdqr"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:singleLine="true"
android:width="300px" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="50px"
android:text="E-mail:" />
<EditText
android:id="@+id/email"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:singleLine="true"
android:width="300px" />
</TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置" />
</LinearLayout>
</TableLayout>
EditTextActivity.java:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class EditTextActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
click();
}
public void click() {
Button btn1 = (Button)this.findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText e = (EditText)findViewById(R.id.nickName);
String nickName = e.getText().toString();
EditText e1 = (EditText)findViewById(R.id.pwd);
String pwd = e1.getText().toString();
EditText e2 = (EditText)findViewById(R.id.pwdqr);
String pwdqr = e2.getText().toString();
EditText email = (EditText)findViewById(R.id.email);
String em = email.getText().toString();
Log.i("编辑框的应用", "会员昵称:" + nickName);
Log.i("编辑框的应用", "密码:" + pwd);
Log.i("编辑框的应用", "密码确认:" + pwdqr);
Log.i("编辑框的应用", "E-mail:" + em);
}
});
}
}
显示效果:
3.2按钮
简介:
Android中提供了普通按钮和图片按钮两种组件,这两种组件都用在UI界面上生成可以单击的按钮。当用户单击按钮时,将触发一个onClick事件,可以通过按钮添加单击事件的监听器,指定所要触发的动作。
在添加两种按钮组件的时候,有两种方式,一种是XML的方式,一个中时Java的方式,推荐使用第一种。
1. 普通按钮
基本语法:
<Button
android:text=”显示文本”
android:id=”@+id/button1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</Button>
在屏幕上添加完按钮后,还需要给按钮添加监听器,才能使按钮发挥其特有的用途。Android提供了两种添加单击事件的监听器的方法。
一种是java代码中完成。如在Activity的onCreate()方法中完成,具体做法如下:
public class Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//添加监听事件
Button btn = (Button)findViewById(R.id.login);
btn.setOnClickListener(new OnClickListener(){
@Override
Public void onClick(View v) {
//要实现的动作代码
}
});
}
}
另一种是在Activity中编写一个包含View类型参数的方法,并且将触发的动作代码置于此方法中,再通过XML属性:android:onClick=”myClick(方法名)”指定对应的方法名实现。如:
public void myClick(View view) {
//编写要执行的动作代码
}
2. 图片按钮
图片按钮与普通按钮使用的方法基本相同,只不过图片按钮使用<ImageButton>标记定义,并且可以为其指定android:src属性,用于设置要显示的图片。
基本语法:
<ImageButton
android:id=”@+id/imageButton1”
android:src=”@drawable/图片文件名”
android:background=”#000”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</ ImageButton>
图片按钮与普通按钮一样,也需要为其添加事件监听,其添加的方式和方法跟普通按钮一致,这里就不在赘述。
代码实例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:text="登录"
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton android:id="@+id/login1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:src="@drawable/login"
android:onClick="myClick"/>
</LinearLayout>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.login);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, "您单击了普通按钮", Toast.LENGTH_SHORT);
toast.show();
}
});
}
public void myClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, "您单击了图片按钮", Toast.LENGTH_SHORT);
toast.show();
}
}
显示效果:
3.3单选按钮和复选框
简介:
单选按钮和复选框都继承了普通按钮,因此,它们都可以直接使用普通按钮支持的各种属性和方法。与普通按钮不同的是,它们提供了可选中的功能。
1. 单选按钮
在默认情况下,单选按钮显示为一个圆形图标,并且在其旁边放置一些说明文字。在程序中,一般将多个单选按钮放置在按钮组中,使这些单选按钮表现出某种功能,当用户选中某个单选按钮后,按钮组中的其他按钮将被自动取消选中状态。在Android中,单选按钮使用RadioButton表示,而RadioButton又是Button的子类,所以单选按钮可以直接使用Button支持的各种属性。
基本语法:
<RadioButton
android:text=”显示文本”
android:id=”@id/ID号”
android:checked=”true|false”
android:layout_width=”wrap_content”
android:layout_height=” wrap_content”>
</RadioButton>
RadioButton组件的android:checked 属性用于指定选中的状态,为true表示选中,false为取消选中,默认值为false
通常情况下,RadioButton需要与RadioGroup组件一起使用,组成一个单选按钮组。在XML布局文件中,添加RadioGroup组件的基本语法为:
<RadioGroup
android:id=”@+id/radioGroup1”
android:orientation=”horizontal”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<!—添加多个RadioButton组件-- >
</RadioGroup>
在屏幕中添加单选按钮组后,还需要获取单选按钮中选中项的值,通常存在以下两种情况,一种是在改变单选按钮组的值时获取,另一种是在单击其他按钮时获取。下面分别介绍两种情况下的实现方法:
在改变单选按钮值时获取
采用此方式时,首先需要获取单选按钮组,然后为其添加OnCheckedChangeListener,并在其onCheckedChanged()方法中获取被选中的单选按钮,并通过其getText()方法获取该单选按钮对应的值,其代码实现如下:
RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton)findViewById(checkedId)
rb.getText(); //获取被选中的单选按钮的值
}
});
单击其他按钮时获取
采用此方式,首先需要在该按钮的单击事件监听器的onClick方法中,通过for循环语句遍历单选按钮组,并根据遍历到的单选按钮的isChecked()方法判断该按钮是否被选中,当被选中时,通过单选按钮的getText()方法获取对应的值。如:
final RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup1);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnCkickListener(){
@Override
public void onClick(View v) {
for(int i=0; i<rg.getChildCount(); i++) {
RadioButton rb = (RadioButton)rg.getChildAt(i);
if(rb.isChecked()) { //判断单选按钮是否处于选中状态
rb.getText(); //获取被选中按钮的值
break; //跳出循环
}
}
}
});
实例代码:
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="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<Button android:id="@+id/button1"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup1);
//为单选按钮组添加事件监听器
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton)findViewById(checkedId);
Log.i("单选按钮", "您选择的是:" + rb.getText());
}
});
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
for(int i=0; i<rg.getChildCount(); i++) {
RadioButton rb = (RadioButton)rg.getChildAt(i);
if(rb.isChecked()) {
Log.i("点击提交", "您选择的是:" + rb.getText());
break;
}
}
}
});
}
}
显示效果:
2. 复选框
在默认情况下,复选框显示为一个方块图标,并且在该图标旁边放置一些说明性文字,与单选按钮唯一的不同是,复选框可以进行多选设置,每一个复选框都提供“选中”和“不选中”两种状态。
在Android中复选框使用CheckBox表示,CheckBox又是Button的子类,所以可以使用Button支持的各种属性。
基本语法:
<CheckBox android:text=”显示文本”
android:id=”@+id/ID号”
androidLlayout_width=”wrap_content”
android:layout_height=”wrap_content”
>
</CheckBox>
由于使用复选框可以选中多项,所以为了确定用户是否选择了某一项,还需要为每个选项添加事件监听,例如:
final CheckBox cb = (CheckBox)findViewById(R.id.like1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoudButton buttonView, boolean isChecked) {
if(cb.isChecked()) { //判断是否被选中
ck.getText(); //获取选中项的值
}
}
});
实例代码:
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="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:"
android:width="100px"
android:gravity="right"
android:height="50px"/>
<CheckBox android:id="@+id/like1"
android:text="体育"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/like2"
android:text="音乐"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/like3"
android:text="美术"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="@+id/button1"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final CheckBox like1=(CheckBox)findViewById(R.id.like1); //获取第1个复选按钮
final CheckBox like2=(CheckBox)findViewById(R.id.like2); //获取第2个复选按钮
final CheckBox like3=(CheckBox)findViewById(R.id.like3); //获取第3个复选按钮
like1.setOnCheckedChangeListener(checkBox_listener); //为like1添加状态改变监听
like2.setOnCheckedChangeListener(checkBox_listener); //为like2添加状态改变监听
like3.setOnCheckedChangeListener(checkBox_listener); //为like3添加状态改变监听
Button button = (Button) findViewById(R.id.button1); //获取提交按钮
//为提交按钮添加单击事件监听
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//通过for循环遍历单选按钮组
String like=""; //保存选中的值
if(like1.isChecked()) //当第一个复选按钮被选中
like+=like1.getText().toString()+" ";
if(like2.isChecked()) //当第一个复选按钮被选中
like+=like2.getText().toString()+" ";
if(like3.isChecked()) //当第一个复选按钮被选中
like+=like3.getText().toString()+" ";
Toast.makeText(MainActivity.this, like, Toast.LENGTH_SHORT).show(); //显示被选中的复选按钮
}
});
}
//创建一个状态改变监听对象
private OnCheckedChangeListener checkBox_listener=new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){ //判断复选按钮是否被选中
Log.i("复选按钮","选中了["+buttonView.getText().toString()+"]");
}
}
};
}
显示效果:
3.4图像视图
简介:
图像视图(ImageView),用于在屏幕中显示任何Drawable对象,通常用来显示图片。在Android中,可以使用两种方法向屏幕中添加图像视图,一种是XML,一种是Java,我们推荐使用前者。
在使用ImageView组件显示图像时,通常可以将要显示的图片放置在res/drawable目录中,然后应用不同的代码将其显示在布局管理器中。
基本语法:
<ImageView
属性列表
>
</ImageView>
ImageView支持的XML属性
android:
adjustViewBounds 用于设置ImageView是否调整自己的边界来保持所显示图片的长度比
android:maxHeight 设置ImageView的最大高度,需要设置
android:adjustViewBounds属性值为true,否则不起作用
android:maxWidth 设置ImageView的最大宽度,需要设置
android:adjustViewBounds属性值为true,否则不起作用
android:scaleType 用于设置所显示图片如何缩放或移动以适应ImageView的大小,其属性值有:
matrix(使用matrix方式进行缩放)
fitXY(对图片横向、纵向独立缩放,使得该图片完全适应于该ImageView,图片的纵横比可能会改变)
fitStart(保持纵横比缩放图片,直到该图片能完全显示在ImageView中,缩放完成后该图片放在ImageView的左上角)
fitCenter(保持纵横比缩放图片,直到该图片能完全显示在ImageView中,缩放完成后该图片放在ImageView的中央)
fitEnd(保持纵横比缩放图片,直到该图片能完全显示在ImageView中,缩放完成后该图片放在ImageView的右下角)
center(把图片放置在ImageView的中间,但不进行任何缩放)
centerCrop(保持纵横比缩放图片,以使得图片能够完全覆盖ImageView)
centerInside(保持纵横比缩放图片,以使得ImageView能完全显示该图片)
android:src 用于设置ImageView所显示的Drawable对象的ID,例如:设置显示保存在res/drawable目录下的名称为flower.jpg的图片,可以将属性设置为android:src=”@drawable/flower”
android:tint 用于为图片着色,其属性值可以是#rgb、#argb、#rrggbb或#aarrggbb表示的颜色值
备注:上面给出的只是该组件的部分属性,关于其他属性可以去Android官方给出的API查找
代码实例:
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="horizontal"
android:background="@drawable/background">
<ImageView android:id="@+id/iv1"
android:src="@drawable/flower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<ImageView android:id="@+id/iv2"
android:src="@drawable/flower"
android:maxWidth="180px"
android:maxHeight="180px"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<ImageView android:id="@+id/iv3"
android:src="@drawable/flower"
android:layout_width="180px"
android:layout_height="180px"
android:scaleType="fitEnd"
android:layout_margin="5px"/>
<ImageView android:id="@+id/iv4"
android:src="@drawable/flower"
android:layout_width="180px"
android:layout_height="180px"
android:tint="#77ff0000"
android:layout_margin="5px"/>
</LinearLayout>
显示效果:
3.5列表选择框(下拉框)
简介:
Android中提供的列表选择框(Spinner)相当于网页中常见的下拉列表框,通常用于提供一系列可选择的列表项供用户进行选择,从而方便用户。
在Android中,可以使用两种方式向屏幕中添加列表选择框,一种是通过XML布局文件中使用的<Spinner>标记,另一种是Java文件中,通过new关键字创建,推荐使用前者。
基本语法:
<Spinner
android:prompt=”@string/info”
android:entries=”@array/数组名称”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/ID号”
>
</Spinner>
其中,android:entries为可选属性,用于指定列表项,如果布局文件中不指定该属性,可以在Java代码中通过为其指定适配器的方式指定;android:prompt属性也是可选属性,用于指定列表选择框的标题
说明:
在Android4.0中,采用默认的主题(Theme.Holo)时,设置anddroid:prompt属性看不到具体的效果,如果采用Theme.Black,就可以在弹出的下拉框中显示该标题
通常情况下,如果列表框中要显示的列表项是可知的,那么可将其保存在数组资源文件中,然后通过数组资源来为列表选择框指定列表项,这样就可以在不编写java代码的情况下实现一个列表选择框。
在屏幕上添加完列表框后,可以使用列表框的getSelectedItem()方法获取列表选择框的选中值:
Spinner spinner = (Spinner)findViewById(R.id.spriner1);
sprinner.getSelectedItem();
在添加完列表选择框后,如果需要在用户选择不同的列表项后,执行相应的处理,则可以为该列表项选择框,添加OnItemSelectedListener事件监听器,如:
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long id){
String result = parent.getItemAtPosition(pos).toString() //获取选择项的值
Log.i(”Spinner示例”,result);
}
@Override
public void onNothingSelected(AdapterView<?> arg0){}
});
在使用列表选择框时,如果不在布局文件中直接为其指定要显示的列表项,也可以通过为其指定适配器的方式指定。具体步骤如下:
1. 创建适配器对象,通常使用ArrayAdapter类。
在Android中创建适配器可以使用以下两种方式:
(1) 通过数组资源文件创建
通过数组资源文件创建适配器,需要使用ArrayAdapter类的createFromResource()方法,具体代码如下:
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.ctype,
android.R.layout.simple_dropdown_item_1line);
(2) 通过使用java文件中的字符串数组创建
使用此方式需要先创建一个一维的字符串数组,用于保存要显示的列表项,然后使用ArrayAdapter类的构造方法
ArrayAdapter(Context context, int textViewResourceId, T[] objects)实例化一个ArrayAdapter类的实例,具体代码如下:
String[] ctype = new String[]{”身份证”,”学生证”,”军人证”};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,ctype);
2. 为适配器设置列表框下拉时的选择样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
3. 将适配器与选择列表关联:
spinner.setAdapter(adapter);
实例代码:
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="horizontal" >
<Spinner android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/ctype"/>
<Spinner android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> adapter, View arg1,
int pos, long arg3) {
String result = adapter.getItemAtPosition(pos).toString();
Log.i("Spinner 示例", result);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//通过适配器来添加下拉列表
String[] items = new String[] {
"男人","女人","动物","水","火"
};
//通过一维数组创建适配器
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, items);
//设置列表下拉框下拉时的选项样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
//将适配器与选择列表框关联
spinner2.setAdapter(adapter);
}
}
显示效果:
3.6列表视图
简介:
列表视图(ListView)是Android中最常用的一种视图组件,它以垂直列表的形式列出需要选择的列表项。如:系统设置、功能内容列表等。在Android中有两种方式可以向屏幕添加列表视图;1.直接使用ListView组件创建;2.让Activity继承ListActivity实现类。
1. 直接使用ListView组件创建
直接使用ListView组件创建,有两种方式,一种是采用XML布局文件中的<ListView>组件,一种是通过java new一个组件的对象,我们推荐使用第一种。
基本语法:
<ListView
属性列表
>
</ListView>
ListView支持的常用的XML属性
android:divider 用于为列表视图设置分隔条,既可以使用颜色分隔,也可以用Drawable资源分隔
android:dividerHeight 设置分隔条的高度
android:entries 通过数组资源为ListView指定列表项
android:footerDividersEnabled 设置是否在footer View之前绘制分隔条,默认值为true,设置为false时表示不绘制。使用该属性时,需要通过ListView组件提供的addFooterView()方法为ListView设置footer View
android:headerDividersEnabled 设置是否在header View之前绘制分隔条,默认值为true,设置为false时表示不绘制。使用该属性时,需要通过ListView组件提供的addHeaderView()方法为ListView设置header View
使用说明:
跟列表选择框一样,设置视图列表的列表项有两种方式:
1. 通过资源文件去添加:
<ListView android:id=”@+id/listView1”
android:entries=”@array/ctype”
android:layout_width=”match_parent”
android:layout_height=”wrap_parent”/>
上面代码使用的是ctype的数组资源,因此需要在res\values目录中创建一个定义数组资源的XML文件array.xml,并在该文件中添加名称为ctype的字符串数组。
<resources>
<string-array name=”ctype”>
…<!—省略了其他项的代码- - >
</string-array>
</resources>
2. 通过适配器去添加
同Spinner列表选择框一样,可以通过为其设置Adapter来指定需要显示的列表项。通过Adapter来为ListView指定要显示的列表项,可分为两个步骤:
第一步:
创建Adapter对象。对于纯文字的列表项,通常使用ArrayAdapter对象。创建ArrayAdapter对象通常可以有两种方式:
(1) 通过数组资源文件创建;
(2) 通过在Java文件中使用字符串数组创建
以上两种方式与Spinner列表选择框中介绍的创建ArrayAdapter的方式基本相同,所不同的就是创建对象时,指定列表项的外观形式不同,主要有如下外观:
sinmple_list_item_1: 每个列表项都是一个普通文本
sinmple_list_item_2: 每个列表项都是一个普通文本(字体略大)
sinmple_list_item_checked: 每个列表项都有一个已选中的列表项
sinmple_list_item_multiple_choise: 每个列表项都是带复选框的文本
sinmple_list_item_single_choise: 每个列表项都是带单选按钮的文本
第二步:
将创建的适配器对象与ListView相关联,可以通过ListView对象的setAdapter()方法实现,具体代码如下:
listView.setAdapter(adapter); //将适配器与ListView关联
为了在点击ListView的各列表项时获取选择项的值,需要为ListView添加OnItemClickListener事件监听器,具体做法如下:
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onIitemClick(AdapterView<?> parent, View arg1, int pos, long id){
String result = parent.getItemAtPosition(pos).toString();
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
});
如果想编写一个列表,前面是图片,后面是标题的列表,如下图:就比较麻烦了.
所以,我们通过一个实例来说明这个效果的制作过程
******************第一步:********************************************
编写main.xml文件,定义ListView视图,如下:
<?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" >
<ListView android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"></ListView>
</LinearLayout>
******************第二步:********************************************
其实,这种带图片的列表,每一个条目(每一行)都是一个利用LinearLayout(线性布局管理器—横向布局)进行布局的,LinearLayout中有两个视图组件,第一个是ImageView,第二个是TextView,这两个组件横向排列,所以,第二步就是制作一个列表视图条目的模板,将这个模板单独放在一个XML文件中,并将XML房间放置在res\layout中,具体如下:
编写items.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView android:id="@+id/image"
android:paddingTop="20px"
android:paddingBottom="20px"
android:paddingRight="10px"
android:adjustViewBounds="true"
android:maxWidth="72px"
android:maxHeight="72px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10px"
android:layout_gravity="center"
android:id="@+id/title"/>
</LinearLayout>
******************第三步:********************************************
编写MainActivity.java文件:
package com.caillc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取列表视图
ListView listView = (ListView) findViewById(R.id.listView1);
// 定义并初始化保存图片id的数组
int[] imgId = new int[] { R.drawable.img01, R.drawable.img02,
R.drawable.img03, R.drawable.img04, R.drawable.img05,
R.drawable.img06, R.drawable.img07, R.drawable.img08 };
// 定义并初始化保存列表文字放到Map中,并添加到List集合中
String[] title = new String[] { "保密设置", "安全", "系统设置", "上网", "我的文档",
"GPS导航", "我的音乐", "E-mail" };
//创建一个List集合
List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
//通过for循环将图片id和列表项文字放到Map中,并添加到List集合中
for (int i = 0; i < imgId.length; i++) {
Map<String, Object> map = new HashMap<String, Object>(); //实例化map对象
map.put("image", imgId[i]);
map.put("title", title[i]);
items.add(map); //将map添加到list中
}
//创建SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.items,
new String[] { "image", "title" }, new int[] {R.id.image, R.id.title
});
//将适配器与ListView关联
listView.setAdapter(adapter);
}
}
运行效果如下:
注:SimpleAdapter类的构造方法SimleAdapter(Context context, List<? extends Map<String,?>> data, int resource, String[] from, int[] to)中:
参数context用于指定关联SimpleAdapter运行的视图上下文;
参数data用于指定一个基于Map的列表,该列表中每个Map(条目)对应列表中的一行(Map中的image代表的是条目中的图片,title代表的是条目中的文字);
参数resource用于指定条目模板(定义列表项目的视图布局文件)的id(唯一标识);
参数from用于指定data参数中每个条目(Map)的key值所组成的数组(将添加到Map上关联每一条目的列名称的数组);
参数to用于指定每个条目模板(items.xml)中定义的组件的id所组成的数组(与参数from显示列对应的视图id的数组)
此适配器的作用是将List<Map<Stirng,Object>>作为列表视图的数据来源(图片和标题),通过from与to参数,建立数据来源与条目模板之间的一一映射关系,即将List<Map<Stirng,Object>>中每个条目的值赋值给模板中的ImageView和TextView,从而最终产生如上图的效果。
实例代码:
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" >
<ListView android:id="@+id/listView1"
android:entries="@array/ctype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
<ListView android:id="@+id/listView2"
android:divider="@drawable/greendivider"
android:dividerHeight="3px"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item>情景模式</item>
<item>主题模式</item>
<item>手机</item>
<item>程序管理</item>
<item>通话设置</item>
<item>连接功能</item>
</string-array>
</resources>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过适配器来添加下拉列表选择框
ListView lv = (ListView)findViewById(R.id.listView2);
//添加header view视图
lv.addHeaderView(line());
String[] items = new String[]{"床前明月光","疑是地上霜","举头望明月","低头思故乡"};
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, items);
lv.setAdapter(adapter);
//设置footer view
lv.addFooterView(line());
//添加监听
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
String result = parent.getItemAtPosition(pos).toString();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
});
}
public View line() {
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.line1);
return iv;
}
}
显示效果:
2. 继承ListActivity实现类
如果程序的窗口仅仅需要显示一个列表,则可以直接让Activity继承ListActivity来实现。继承了ListActivity的类中,无需调用setContentView()方法来显示页面,而是可以直接为其设置适配器,从而显示一个列表
在onCreate方法中创建作为列表项的Adapter,并使用setListAdapter()方法将其添加到列表中。
public class MainActivity extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/************创建用于为ListView指定列表项的适配器************/
String[] ctype = new String[]{”情景模式”,”主题模式”,”手机”,”程序管理”};
ArrayAadpter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,ctype);
setListAdapter(adapter);
}
}
为了在单击ListView的各列表项时获取选项的值,需要重新重写父类中的onListItemClick()方法
@Override
protected void onListItemClick(ListView l,View v, int position, long id){
super.onListItemClick(I,v,position,id);
String result = l.getItemAtPosition(position).toString(); //获取选择项的值
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
3.7日期、时间拾取器
简介:
为了让用户能够选择日期和时间,Android提供了日期、时间拾取器,分别是DatePicker组件和TimePicker组件,这两个组件使用比较简单。为了可以在程序中获取用户选择的日期、时间,还需要为DatePicker和TimePicker组件添加事件监听器。其中,DatePicker组件对应的事件监听器是OnDateChangedListener而TimePicker对应的事件监听器是OnTimeChangedListener。
基本语法:
<DatePicker android:id=”@+id/datePicker1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
<TimePicker android:id=”@+id/datePicker1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
在主活动中MainActivity的onCreate方法中,获取日期拾取器和时间拾取器组件,并将时间拾取器组件设置为24小时制式显示:
DatePicker datePicker = (DatePicker)findVidwById(R.id.datePicker1);
TimePickerPicker time = (TimePicker)findVidwById(R.id.datePicker1);
time.setIs24HourView(true);
初始化日期拾取器组件,同时为其设置OnDateChangedListener事件监听器,以及为时间拾取组件添加事件监听器:
datePicker.init(year,month,day,new OnDateChangedListener(){
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute){
MainActivity.this.year = year; //year为当前年份
MainActivity.this.month = month; //year为当前年份
MainActivity.this.day = day; //year为当前年份
show(year,month,day,hour,minute); //通过消息框显示日期和时间
}
timepicker.setOnTimeChangeListener(new OnTimeChangedListener(){
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
MainActivity.this.hour = hourOfDay; //改变hour属性的值
MainActivity.this.minute=minute;
show(year,month,day,hourOfDay,minute);
}
});
});
注:通过DatePicker对象获取的月份是0~11月,而不是1-12月,所以需要将获取的结果加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="horizontal" >
<DatePicker android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TimePicker android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.caillc;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private int year; // 年
private int month;
private int day;
private int hour;
private int minute;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1); // 获取日期拾取组件
TimePicker timepicker = (TimePicker) findViewById(R.id.timePicker1); // 获取时间拾取组件
timepicker.setIs24HourView(true);
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR); // 获取当前年份
month = calendar.get(Calendar.MONTH); // 获取当前月份
day = calendar.get(Calendar.DAY_OF_MONTH); // 获取当前日
hour = calendar.get(Calendar.HOUR_OF_DAY); // 获取当前小时数
minute = calendar.get(Calendar.MINUTE); // 获取当前分钟数
timepicker.setCurrentHour(hour); //设置当前的小时数(后添加的)
timepicker.setCurrentMinute(minute); //设置当前的分钟数(后添加的)
// 初始化日期拾取器,并在初始化时指定监听器
datepicker.init(year, month, day, new OnDateChangedListener() {
public void onDateChanged(DatePicker arg0, int year, int month,
int day) {
MainActivity.this.year = year; // 改变year属性的值
MainActivity.this.month = month; // 改变month属性的值
MainActivity.this.day = day; // 改变day属性的值
show(year, month, day, hour, minute); // 通过消息框显示日期时间
}
});
// 为时间拾取器设置监听器
timepicker.setOnTimeChangedListener(new OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
MainActivity.this.hour = hourOfDay; // 改变hour属性的值
MainActivity.this.minute = minute; // 改变minute属性的值
show(year, month, day, hourOfDay, minute); // 通过消息框显示选择的日期时间
}
});
}
private void show(int year, int month, int day, int hour, int minute) {
String str = year + "年" + (month+1) + "月" + day + "日 " + hour + ":"
+ minute; // 获取拾取器设置的日期和时间
Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); // 显示消息提示框
}
}
显示效果:
3.8计时器
简介:
计时器(Chronometer)组件可显示从某个起始时间开始,一共过去了多长时间的文本。由于该组件继承自TextView,所以它以文本的形式显示内容。使用该组件比较简单,通常情况下只需要使用以下5个方法:
1. setBase():用于设置计时器的起始时间
2. setFormat():用于设置显示时间的格式
3. start():指定开始计时
4. stop():指定停止计时
5. setOnChronometerTickListener():为计时器绑定事件监听器,当计时器改变时触发该监听器。
基本语法:
<Chronomeer
android:text=”Chronometer”
android:id=”@+id/chronometer1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
在主活动MainActivity的onCreate方法中获取计时器组件,并设置起始时间和显示时间格式、开启计时器,以及为其添加监听器
final Chronometer ch =
(Chronometer)findViewById(R.id.chronometer1); //获取计时器组件
ch.setBase(SsytemClock.elapsedRealtime());
ch.setFormat(”已用时间:%s”); //设置显示时间格式
//添加监听事件
ch.setOnChronometerTickListener(new OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer chronometer) {
if(SystemClock.elapsedRealtime()-ch.getBase()>=10000){
ch.stop(); //停止时间
}
}
});
示例代码:
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" >
<Chronometer
android:text="Chronometer"
android:id="@+id/chronometer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Chronometer c = (Chronometer)findViewById(R.id.chronometer1);
c.setBase(SystemClock.elapsedRealtime()); //设置起始时间
c.setFormat("已用时间: %s"); //设置显示时间的格式
c.start(); //开启计时器
c.setOnChronometerTickListener(new OnChronometerTickListener(){
public void onChronometerTick(Chronometer view) {
if(SystemClock.elapsedRealtime()-c.getBase() >= 10000) {
c.stop(); //停止计时器
}
}
});
}
}
显示效果:
一、 Android环境
1.环境简介
2.环境搭建
3.Android项目结构说明
二、用户界面设计
1.UI界面控制
简介:
用户界面设计时Android应用开发的一个重要内容。在进行用户界面设计时,首先要了解页面中的UI元素如何呈现给用户,也就是如何控制UI界面,Android提供了4种控制UI界面的方法
方法分类:
1. 使用XML布局文件控制UI界面
Android提供了一种非常简单、方便的方法用于控制UI界面。该方法采用XML文件来进行界面布局,从而将布局界面的代码和逻辑控制的java代码分离开来,使程序的结构更加清晰、明了。
具体步骤:
(1) 在Android应用的res/layout目录下面编写XML布局文件,可以采用任何符合java命名规则的文件名,创建后R.java会自动收录布局资源的。
(2) 在Activity中使用以下Java代码显示XML文件中布局的内容
setContentView(R.layout.main)
注:main是XML布局文件的文件名
通过上面两个步骤就可以轻松实现布局并显示UI界面的功能。
具体事例:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:backgourd="@drawable/background">
<!-- 添加提示文字 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/title"
style="@style/text"/>
<!-- 添加开始按钮 -->
<TextView
android:id="@+id/startButton"
android:layout_gravity="center_horizontal|center_vertical"
android:text="@string/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/text"/>
</FrameLayout>
此布局管理器采用的是帧布局(FrameLayout)并且添加了两个TextView组件,第一个用于显示文件,第2个用于在窗体的正中间位置显示开始游戏按钮
在布局管理文件main.xml中通过设置布局管理器的android:backgourd属性可以为窗体设置背景图片,通过设置具体组件的style,可以为组件设置样式,使用android:layout_gravity="center_horizontal|center_vertical"可以让该组件在帧布局中居中显示
修改res/values目录下的string.xml文件,并在该文件中添加一个用于定义开始按钮内容的常量,名为start,内容为“单机开始游戏…”如下图:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, XMLLayoutActivity!</string>
<string name="app_name">1.2</string>
<string name="title">使用XML布局文件控制UI界面</string>
<string name="start">单机开始游戏...</string>
</resources>
string.xml文件用于定义应用程序中的字符串常量。其中,meiyige<string>子元素都可以定义一个字符串常量,常量名称有name属性指定,常量内容写在起始标签和结束标签之间
为了改变窗口中文字的大小,需要为TextView组件添加style样式属性,用于指定应用的样式,具体的样式需要在res/values目录中创建的样式文件中指定,如创建一个名为styles.xml的样式文件,并在该文件中创建一个名称为text的样式,用于指定文字的大小和颜色,具体内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="text">
<item name="android:textSize">24px</item>
<item name="android:textColor">#f8f8f8</item>
</style>
</resources>
在主活动(MainActivity)中,应用以下代码,指定活动应用的布局文件:
setContentView(R.layout.main);
具体运行结果如下:
2. 在代码中控制UI界面
简介:
Android支持像Java Swing那样完全通过代码控制UI界面,也就是所有的UI界面通过new关键字创建出来,然后将UI组件添加到布局管理器中,从而实现用户界面
具体步骤:
(1) 创建布局管理器,可以使帧布局、表格布局、线性布局和相对布局,并设置布局管理器的属性
(2) 创建具体组件,可以使TextView、ImageView、EditText和Button等Android提供的组件,并设置组件的布局和各种属性
(3) 将创建的组件添加到布局管理其中
特点:
完全通过代码控制UI界面虽然比较灵活,但是其开发过程比较繁琐,而且不利于高层次的解耦,因此不推荐采用这种方式控制UI界面。
具体事例:
package com.caillc;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;
public class JavaLayoutActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
// 创建布局管理器
FrameLayout frameLayout = new FrameLayout(this);
setContentView(frameLayout);
// 创建TextView组件
TextView tv = new TextView(this);
tv.setText("在代码中控制UI界面");
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
tv.setTextColor(Color.rgb(25, 255, 25));
// 添加组件到布局管理器
frameLayout.addView(tv);
// 创建按钮组件
TextView tw = new TextView(this);
tw.setText("点击进入游戏...");
tw.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
tw.setTextColor(Color.rgb(25, 255, 25));
// 创建一个布局参数
LayoutParams params = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
// 设置布局参数
tw.setLayoutParams(params);
// 给组件添加时间监听器
tw.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 设置对话框
new AlertDialog.Builder(JavaLayoutActivity.this)
.setTitle("系统提示")
.setMessage("游戏有风险,进入需谨慎,真的要进入吗?")
// 设置对话框的内容
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log.i("3.1.2", "进入游戏");// 输入日志
}
})
.setNegativeButton("退出",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Log.i("3.1.2", "退出游戏");
finish(); // 结束游戏
}
}).show(); // 显示对话框
}
});
//添加组件
frameLayout.addView(tw);
}
}
代码中有注释可以自己看
显示效果:
点击按钮:
点击“确定”对话框消失,点击退出直接退出应用
3. 使用XML和Java代码混合控制UI界面
简介:
完全通过XML布局文件控制UI界面,实现比较方便快捷,但是有失灵活;而完全采用java代码控制UI界面,虽然比较灵活,但是开发过程比较繁琐,鉴于两种方法的优缺点,可以融合两种方式,同时采用两种方式进行UI界面的控制。
使用XML和java控制UI界面,习惯上把变化小,行为比较固定的组件放在XML布局文件中,把变化较多,行为控制比较复杂的组件交给Java代码来管理
具体事例:
第一步修改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="horizontal"
android:background="@drawable/background"
android:id="@+id/layout">
</LinearLayout>
第二步在主活动中添加组件:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class JavaAndXMLActivity extends Activity {
private ImageView[] img = new ImageView[4];
private int[] imagePath = new int[] {
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout)this.findViewById(R.id.layout);
for(int i=0; i<imagePath.length; i++) {
img[i] = new ImageView(this);
img[i].setImageResource(imagePath[i]);
img[i].setPadding(2, 2, 2, 2);
LayoutParams params = new LayoutParams(253,148);
img[i].setLayoutParams(params);
layout.addView(img[i]);
}
}
}
运行效果如下:
4. 开发自定义的View
简介:
在Android中所有的UI界面都是由View类和ViewGroup类及子类组合而成的,其中View是所有类的基类,而ViewGroup类是容纳这些UI组件的容器,其本身也是View类的子类。在ViewGroup类中,除了可以包含普通的View类外,还可以再次包含ViewGroup类。
操作步骤:
(1) 创建一个继承android.view.View类的View类,并且重写构造方法。
(2) 根据需要,重写相应的方法。
(3) 在项目的活动中,创建并实例化自定义的View类,将其添加到布局管理器中。
具体事例(无)
3.SDK中常用命令
2布局管理器
2.1简介
每个组件在窗体中都有具体的位置和大小,在窗体中摆放各种组件时很难进行判断。使用Android布局管理器可以很方便的控制各组件的位置和大小。
Android中提供了“线性布局管理器(LinearLayout)”、“表格布局管理器(TableLayout)”、“帧布局管理器(FrameLayout)”、“相对布局管理器(RelativeLayout)”和“绝对布局管理器(AbsoluteLayout)”。其中最后一种绝对布局管理器在Android2.0中被标记为已过期,可以使用帧布局或相对布局替代。
2.2线性布局
简介:
“线性布局管理器”是将放入其中的组件垂直或水平方向来布局,也就是控制放入其中组件横向或纵向排列。
特点:
在线性布局中,每一行(针对垂直排列)或每一列(针对水平排列)中,只能放一个组件,并且Android的线性布局不会换行,当组件排列到窗口边缘后,后面的组件将不会被显示出来。
使用说明:
1. 使用XML布局文件定义线性布局管理器
代码实例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:id="@+id/layout">
</LinearLayout>
代码分解说明:
XML属性 描述
linearLayout
在XML布局管理器中定义线性管理器时,需要使用<linearLayout>标记,其基本的语法格式如下:
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
属性列表
>
</LinearLayout>
android:orientation 用于设置布局管理器内组件的排列方式,其可选值有两种:horizontal:表示水平排列;
vertical:表示垂直排列;
android:gravity 用于设置布局管理器内组件的对齐方式,其可选值包括:
top: 靠上对齐
bottom: 靠下对齐
left: 靠左对齐
right: 靠右对齐
center_vertical: 垂直居中
fill_vertical:增加组件的垂直尺寸以在垂直方向填满容器。
center_horizontal: 水平居中
fill_horizontal: 增加组件的水平尺寸以在水平方向填满容器。
center: 垂直和水平方式都居中,不改变组件大小
fill:水平和垂直方向充满容器
clip_vertical:将组件的垂直方向的上下边向容器靠拢
clip_horizongtal: 将组件的水平方向的左右边向容器靠拢
这些属性也可以同时指定,各属性之间用竖线“|”隔开,如:
right|bottom:指定组件靠右下角对齐
android:
layout_width 设置组件的基本宽度,其值可选如下:
fill_parent:表示该组件的宽度与容器的宽度相同;
match_parent:与fill_parent的作用完全相同,从Android2.2开始推荐使用;
wrap_content:表示该组件的宽度恰好能包住组件的内容;
android:
layout_height 设置组件的基本高度,其值可选如下:
fill_parent:表示该组件的高度与容器的宽度相同;
match_parent:与fill_parent的作用完全相同,从Android2.2开始推荐使用;
wrap_content:表示该组件的高度恰好能包住组件的内容;
android:id 用于为当前组件指定一个id属性;在java代码中可以单独引用这个组件。为组件指定id后,在R.java文件中,会自动派生一个对应的属性,在java代码中,可以通过findViewById()方法来获取它
android:
backgroud 为组件设置背景,可以是背景图片,也可以是背景颜色,为组件指定背景图片时,可以将准备好的背景图片复制到目录res/drawable-hdpi或
res/drawable-ldpi或
res/drawable-mdpi下,然后使用下面代码进行设置引用:
android:background="@drawable/background"其中@drawable指的是上面的目录,而background则表示图片的名称。
如果想指定背景颜色,可以使用颜色值如:
android:background="#ffffff"
备注: 1.属性android:orientation和android:gravity是线性布局管理器支持的属性,其他几个属性都是android.view.View和android.view.ViewGroup支持的属性
2.属性android:layout_width和android:layout_height是ViewGroup.LayoutParams所支持的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="horizontal"
android:background="@drawable/background"
android:id="@+id/layout" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="按钮1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="按钮2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="按钮3" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="按钮4" />
</LinearLayout>
效果:
代码:
<?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" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮4" />
</LinearLayout>
效果:
2. 使用Java代码来创建(不建议使用)
2.3表格布局
简介:
表格布局与常见的表格布局类似,以行、列的形式来管理放入其中的UI组件。表格布局适用<TableLayout>标记定义,在表格布局中,可以添加多个<TableRow>标记,每一个<TableRow>标记占用一行,其中<TableRow>也是容器,所以还可以在它其中添加其他组件,每添加一个组件,表格就会增加一列。
特点:
在表格布局中,列可以别隐藏,也可以被设置为伸展的,从而填充可利用的屏幕空间,还可以设置为强制收缩,直到匹配屏幕大小
如果在表格布局中,直接向<TableLayout>中添加UI组件,那么该组件将独占一行。
表格布局管理器,可以使用XML布局文件中定义的表格布局管理器,也可以使用Java代码创建,不过推荐前者。
TableLayout继承了LinearLayout,因此,它完全支持LinearLayout所支持的全部的XML属性,此外它还有自己特有的XML属性。
使用说明:
代码实例:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3E3C89"
android:gravity="center_vertical"
android:stretchColumns="0,3">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView />
<TextView android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24px"
android:id="@+id/textView1"/>
<EditText android:id="@+id/editText1"
android:textSize="24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200px"/>
<TextView />
</TableRow>
</TableLayout>
特色属性说明
XML属性 描述
android:stretchColumns 设置允许被拉伸的列的列号(序号从0开始),多个序号之间用逗号“,”分割
android:collapseColumns 设置允许被隐藏的列的列号(序号从0开始),多个序号之间用逗号“,”分割
android:shrinkColumns 设置允许被收缩的列的列号(序号从0开始),多个序号之间用逗号“,”分割
具体事例:
实例代码:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#3E3C89"
android:gravity="center_vertical"
android:stretchColumns="0,3">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView />
<TextView android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24px"
android:id="@+id/textView1"/>
<EditText android:id="@+id/editText1"
android:textSize="24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200px"/>
<TextView />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView />
<TextView android:text="用户名:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24px"
android:id="@+id/textView2"/>
<EditText android:id="@+id/editText2"
android:textSize="24px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200px"
android:inputType="textPassword" />
<TextView />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView />
<Button android:text="登录"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:text="登录"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView />
</TableRow>
</TableLayout>
实例效果:
2.4帧布局
简介:
在帧布局管理器中,每加入一个组件,都降创建一个空白的区域,通常称为一帧,这些帧都会根据gravity属性执行自动对齐,默认情况下,帧布局从屏幕的左上角(0,0)坐标点开始布局,多个组件层叠排序,后面的组件覆盖前面的组件。
可以通过XML和java两种方式定义帧布局管理器,我们推荐前者
属性:
XML属性 描述
android:foreground 设置该帧布局容器的前景图像
android:foregroundGravity 定义绘制前景图像的gravity属性,即前景图像显示的位置。
备注:
帧布局管理器经常应用在游戏开发中,用于显示自定义的视图
具体事例:
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:foreground="@drawable/ic_launcher"
android:foregroundGravity="bottom|right">
<!-- 添加居中显示的红色背景的TextView,将显示在下面 -->
<TextView android:text="红色背景的TextView"
android:id="@+id/textView1"
android:background="#ffff0000"
android:layout_gravity="center"
android:layout_width="400px"
android:layout_height="400px"/>
<!-- 添加居中显示的橙色背景的TextView,将显示在下面 -->
<TextView android:text="红色背景的TextView"
android:id="@+id/textView1"
android:background="#ffff6600"
android:layout_gravity="center"
android:layout_width="300px"
android:layout_height="300px"/>
<!-- 添加居中显示的黄色背景的TextView,将显示在下面 -->
<TextView android:text="红色背景的TextView"
android:id="@+id/textView1"
android:background="#ffffee00"
android:layout_gravity="center"
android:layout_width="200px"
android:layout_height="200px"/>
</FrameLayout>
显示效果:
2.5 相对布局
简介:
相对布局是指按照组件之间的相对位置来进行布局,如某一个组件在另外一个组件的左边、右边、上边或者下边。相对布局使用标签<RelativeLayout>
使用相对布局有两种方式:XML和Java编码,我们推荐使用前者.
属性:
XML属性 描述
android:gravity 用于设置布局管理器中各组件的对齐方式
Android:ignoreGravity 用于指定哪个组件不受gravity属性的影响
在相对布局管理器中,只有上面介绍的两个属性是不够的,为了更好的控制布局管理器中各子组件的布局分布,RelativeLayout提供了一个内部类RelativeLayout.LayParams,通过该类提供大量的XML属性,可以很好的控制相对管理器中各组件的分布方式。其具体属性如下
XML属性 描述
android:layout_above 其属性值为其他UI组件的id属性,用于指定该组件位于哪个组件的上方
android:layout_below 其属性值为其他UI组件的id属性,用于指定该组件位于哪个组件的下方
android:layout_toLeftOf 其属性值为其他UI组件的id属性,用于指定该组件位于哪个组件的左侧
android:layout_toRightOf 其属性值为其他UI组件的id属性,用于指定该组件位于哪个组件的右侧
android: layout_alignTop 其属性值为其他UI组件的id属性,用于指定该组件与哪个组件的上边界对齐
android: layout_alignBottom 其属性值为其他UI组件的id属性,用于指定该组件与哪个组件的下边界对齐
android: layout_alignLeft 其属性值为其他UI组件的id属性,用于指定该组件与哪个组件的左边界对齐
android: layout_alignRight 其属性值为其他UI组件的id属性,用于指定该组件与哪个组件的右边界对齐
android:layout_alignParentTop 其属性值为boolean值,用于指定该组件是否与布局管理器的顶端对齐
android:layout_alignParentBottom 其属性值为boolean值,用于指定该组件是否与布局管理器的底端对齐
android:layout_alignParentLeft 其属性值为boolean值,用于指定该组件是否与布局管理器的左边对齐
android:layout_alignParentRight 其属性值为boolean值,用于指定该组件是否与布局管理器的右边对齐
android: layout_centerHorizontal 其属性值为boolean值,用于指定该组件是否位于布局管理器水平居中的位置
android: layout_centerVertical 其属性值为boolean值,用于指定该组件是否位于布局管理器垂直居中的位置
android: layout_centerInParent 其属性值为boolean值,用于指定该组件是否位于布局管理器中央位置
具体事例:
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/relativeLayout1"
android:background="@drawable/background">
<!-- 添加一个居中显示的文本视图 -->
<TextView android:text="发现有Widget的新版本,您想现在就安装吗?"
android:id="@+id/textView1"
android:textSize="24px"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<!-- 添加一个在button2左侧显示的按钮button1 -->
<Button android:id="@+id/button1"
android:text="现在更新"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_toLeftOf="@+id/button2"/>
<!-- 添加一个在button2 该按钮与textView1的右边界对齐 -->
<Button android:id="@+id/button2"
android:text="现在更新"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_alignRight="@+id/textView1"/>
</RelativeLayout>
显示效果:
3.基本组件
3.1文本框和编辑框
简介:
Android提供了两种文本组件:一种是文本框(TextView),用于屏幕上显示文本,相当于Java中的标签,也就是JLable;另一种是编辑框(EditText)用于在屏幕上显示可编辑的文本;其中EditText是TextView的子类。
向屏幕添加文本框有两种方式,一种是XML,一种是通过Java方式new,我们推荐使用第一种。
1. 文本框
基本语法:
<TextView
属性列表
>
</TextView>
TextView常用的XML属性
android:autoLink 用于指定是否将指定格式的文本转换为可单击的超链接形式,其属性值有none、web、email、phone、map和all
android:drawableBottom 用于在文本框内文本的底端绘制指定图像、该图像可以是放在res\drawable目录下的图片,通过“@drawable/文件名(不包括文件扩展名)”设置
android:drawableLeft 用于在文本框内文本的左侧绘制指定图像、该图像可以是放在res\drawable目录下的图片,通过“@drawable/文件名(不包括文件扩展名)”设置
android: drawableRight 用于在文本框内文本的右侧绘制指定图像、该图像可以是放在res\drawable目录下的图片,通过“@drawable/文件名(不包括文件扩展名)”设置
android: drawableTop 用于在文本框内文本的上端绘制指定图像、该图像可以是放在res\drawable目录下的图片,通过“@drawable/文件名(不包括文件扩展名)”设置
android:gravity 用于设置文本框内文本的对齐方式,可选值有top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、和clip_horizontal等。这些属性值也可以同时指定,各属性值之间用竖线隔开。例如要指定组件内容靠右下角对齐,可以使用属性right|bottom.
android:hint 用于设置文本框为空时,默认显示的提示文本
android:inputType 用于指定当前文本框显示内容的文本类型,其可选值有textPassword、textEmailAddress、phone、date等,可以同时指定多个,使用“|”分隔
android:singleLine 用于指定该文本框是否为单行模式,其属性为有true和false,为true时表示文本框不会换行,当文本内容超过一行时,超过的部分将被省略,同时再结尾处添加“…”
android:text 用于指定文本框内显示的文本内容,可以直接在该属性值中指定,也可以通过string.xml文件中定义的文本常量的方式指定
android:textColor 用于设定文本内容的颜色,其属性值可以是#rgb、#argb、#rrggbb或#aarrggbb格式指定的颜色
android:textSize 用于指定文本文字的大小,其属性由代表大小的数值和单位组成,其单位可以有px、pt、sp和in等
android:width 用于指定文本框的宽度,单位为像素
android:height 用于指定文本框的高度,单位为像素
备注:上面的属性只是TextView组件的部分常用的属性,关于该组件的其他属性,可以参考Android官方提供的API文档。
代码实例:
<?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="@drawable/background">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:autoLink="email"
android:height="50px"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上方带图片的TextView"
android:drawableTop="@drawable/icon"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左侧带图片的TextView"
android:drawableLeft="@drawable/icon"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下方带图片的TextView"
android:drawableBottom="@drawable/icon"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多行文字,1979年,那是一个春天啊,有一位老人在中国的南海边画了一个圈,生活变得不一样啊!"
android:width="300px"
android:textSize="20px"
android:layout_marginBottom="20px"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多行文字,1979年,那是一个春天啊,有一位老人在中国的南海边画了一个圈,生活变得不一样啊!"
android:singleLine="true"
android:width="300px"
android:textSize="20px"/>
</LinearLayout>
效果展示:
2. 编辑框
Android中的编辑框可以输入单行文字,也可以输入多行文本,还可以输入指定格式的文本(如密码、电话号码、E-mail地址等)
基本语法:
<EditText
属性列表
>
</<EditText>
支持XML的属性:
由于EditText类是TextView的子类,所以针对TextView的所有的属性,EditText都可以使用,需要注意的是EditText组件中,android:inputType属性可以帮助输入框显示合适的类型。如输入的格式如果是密码,则其值可以是textPassword
获取编辑框的内容:
在屏幕中添加了编辑框后,还需要获取编辑框中的内容,这里可以通过编辑框组件提供的getText()方法实现。使用该方法时,先要获取到编辑框的组件,然后再调用getText()方法,如:获取布局文件中id为login的编辑框的内容,可以通过以下代码实现:
EditText login = (EditText)findViewById(R.id.login);
String loginText = login.getText().toString();
代码实例:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="50px"
android:text="会员昵称:" />
<EditText
android:id="@+id/nickName"
android:layout_height="wrap_content"
android:hint="请输入会员昵称"
android:singleLine="true"
android:width="300px" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="50px"
android:text="输入密码:" />
<EditText
android:id="@+id/pwd"
android:layout_height="wrap_content"
android:hint="请输入会员昵称"
android:inputType="textPassword"
android:singleLine="true"
android:width="300px" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="50px"
android:text="密码确认:" />
<EditText
android:id="@+id/pwdqr"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:singleLine="true"
android:width="300px" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:height="50px"
android:text="E-mail:" />
<EditText
android:id="@+id/email"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:singleLine="true"
android:width="300px" />
</TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置" />
</LinearLayout>
</TableLayout>
EditTextActivity.java:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class EditTextActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
click();
}
public void click() {
Button btn1 = (Button)this.findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText e = (EditText)findViewById(R.id.nickName);
String nickName = e.getText().toString();
EditText e1 = (EditText)findViewById(R.id.pwd);
String pwd = e1.getText().toString();
EditText e2 = (EditText)findViewById(R.id.pwdqr);
String pwdqr = e2.getText().toString();
EditText email = (EditText)findViewById(R.id.email);
String em = email.getText().toString();
Log.i("编辑框的应用", "会员昵称:" + nickName);
Log.i("编辑框的应用", "密码:" + pwd);
Log.i("编辑框的应用", "密码确认:" + pwdqr);
Log.i("编辑框的应用", "E-mail:" + em);
}
});
}
}
显示效果:
3.2按钮
简介:
Android中提供了普通按钮和图片按钮两种组件,这两种组件都用在UI界面上生成可以单击的按钮。当用户单击按钮时,将触发一个onClick事件,可以通过按钮添加单击事件的监听器,指定所要触发的动作。
在添加两种按钮组件的时候,有两种方式,一种是XML的方式,一个中时Java的方式,推荐使用第一种。
1. 普通按钮
基本语法:
<Button
android:text=”显示文本”
android:id=”@+id/button1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</Button>
在屏幕上添加完按钮后,还需要给按钮添加监听器,才能使按钮发挥其特有的用途。Android提供了两种添加单击事件的监听器的方法。
一种是java代码中完成。如在Activity的onCreate()方法中完成,具体做法如下:
public class Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//添加监听事件
Button btn = (Button)findViewById(R.id.login);
btn.setOnClickListener(new OnClickListener(){
@Override
Public void onClick(View v) {
//要实现的动作代码
}
});
}
}
另一种是在Activity中编写一个包含View类型参数的方法,并且将触发的动作代码置于此方法中,再通过XML属性:android:onClick=”myClick(方法名)”指定对应的方法名实现。如:
public void myClick(View view) {
//编写要执行的动作代码
}
2. 图片按钮
图片按钮与普通按钮使用的方法基本相同,只不过图片按钮使用<ImageButton>标记定义,并且可以为其指定android:src属性,用于设置要显示的图片。
基本语法:
<ImageButton
android:id=”@+id/imageButton1”
android:src=”@drawable/图片文件名”
android:background=”#000”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</ ImageButton>
图片按钮与普通按钮一样,也需要为其添加事件监听,其添加的方式和方法跟普通按钮一致,这里就不在赘述。
代码实例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button android:text="登录"
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageButton android:id="@+id/login1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:src="@drawable/login"
android:onClick="myClick"/>
</LinearLayout>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.login);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, "您单击了普通按钮", Toast.LENGTH_SHORT);
toast.show();
}
});
}
public void myClick(View v) {
Toast toast = Toast.makeText(MainActivity.this, "您单击了图片按钮", Toast.LENGTH_SHORT);
toast.show();
}
}
显示效果:
3.3单选按钮和复选框
简介:
单选按钮和复选框都继承了普通按钮,因此,它们都可以直接使用普通按钮支持的各种属性和方法。与普通按钮不同的是,它们提供了可选中的功能。
1. 单选按钮
在默认情况下,单选按钮显示为一个圆形图标,并且在其旁边放置一些说明文字。在程序中,一般将多个单选按钮放置在按钮组中,使这些单选按钮表现出某种功能,当用户选中某个单选按钮后,按钮组中的其他按钮将被自动取消选中状态。在Android中,单选按钮使用RadioButton表示,而RadioButton又是Button的子类,所以单选按钮可以直接使用Button支持的各种属性。
基本语法:
<RadioButton
android:text=”显示文本”
android:id=”@id/ID号”
android:checked=”true|false”
android:layout_width=”wrap_content”
android:layout_height=” wrap_content”>
</RadioButton>
RadioButton组件的android:checked 属性用于指定选中的状态,为true表示选中,false为取消选中,默认值为false
通常情况下,RadioButton需要与RadioGroup组件一起使用,组成一个单选按钮组。在XML布局文件中,添加RadioGroup组件的基本语法为:
<RadioGroup
android:id=”@+id/radioGroup1”
android:orientation=”horizontal”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
<!—添加多个RadioButton组件-- >
</RadioGroup>
在屏幕中添加单选按钮组后,还需要获取单选按钮中选中项的值,通常存在以下两种情况,一种是在改变单选按钮组的值时获取,另一种是在单击其他按钮时获取。下面分别介绍两种情况下的实现方法:
在改变单选按钮值时获取
采用此方式时,首先需要获取单选按钮组,然后为其添加OnCheckedChangeListener,并在其onCheckedChanged()方法中获取被选中的单选按钮,并通过其getText()方法获取该单选按钮对应的值,其代码实现如下:
RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup1);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton)findViewById(checkedId)
rb.getText(); //获取被选中的单选按钮的值
}
});
单击其他按钮时获取
采用此方式,首先需要在该按钮的单击事件监听器的onClick方法中,通过for循环语句遍历单选按钮组,并根据遍历到的单选按钮的isChecked()方法判断该按钮是否被选中,当被选中时,通过单选按钮的getText()方法获取对应的值。如:
final RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup1);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnCkickListener(){
@Override
public void onClick(View v) {
for(int i=0; i<rg.getChildCount(); i++) {
RadioButton rb = (RadioButton)rg.getChildAt(i);
if(rb.isChecked()) { //判断单选按钮是否处于选中状态
rb.getText(); //获取被选中按钮的值
break; //跳出循环
}
}
}
});
实例代码:
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="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<Button android:id="@+id/button1"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final RadioGroup rg = (RadioGroup)findViewById(R.id.radioGroup1);
//为单选按钮组添加事件监听器
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton)findViewById(checkedId);
Log.i("单选按钮", "您选择的是:" + rb.getText());
}
});
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
for(int i=0; i<rg.getChildCount(); i++) {
RadioButton rb = (RadioButton)rg.getChildAt(i);
if(rb.isChecked()) {
Log.i("点击提交", "您选择的是:" + rb.getText());
break;
}
}
}
});
}
}
显示效果:
2. 复选框
在默认情况下,复选框显示为一个方块图标,并且在该图标旁边放置一些说明性文字,与单选按钮唯一的不同是,复选框可以进行多选设置,每一个复选框都提供“选中”和“不选中”两种状态。
在Android中复选框使用CheckBox表示,CheckBox又是Button的子类,所以可以使用Button支持的各种属性。
基本语法:
<CheckBox android:text=”显示文本”
android:id=”@+id/ID号”
androidLlayout_width=”wrap_content”
android:layout_height=”wrap_content”
>
</CheckBox>
由于使用复选框可以选中多项,所以为了确定用户是否选择了某一项,还需要为每个选项添加事件监听,例如:
final CheckBox cb = (CheckBox)findViewById(R.id.like1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoudButton buttonView, boolean isChecked) {
if(cb.isChecked()) { //判断是否被选中
ck.getText(); //获取选中项的值
}
}
});
实例代码:
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="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好:"
android:width="100px"
android:gravity="right"
android:height="50px"/>
<CheckBox android:id="@+id/like1"
android:text="体育"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/like2"
android:text="音乐"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/like3"
android:text="美术"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="@+id/button1"
android:text="提交"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final CheckBox like1=(CheckBox)findViewById(R.id.like1); //获取第1个复选按钮
final CheckBox like2=(CheckBox)findViewById(R.id.like2); //获取第2个复选按钮
final CheckBox like3=(CheckBox)findViewById(R.id.like3); //获取第3个复选按钮
like1.setOnCheckedChangeListener(checkBox_listener); //为like1添加状态改变监听
like2.setOnCheckedChangeListener(checkBox_listener); //为like2添加状态改变监听
like3.setOnCheckedChangeListener(checkBox_listener); //为like3添加状态改变监听
Button button = (Button) findViewById(R.id.button1); //获取提交按钮
//为提交按钮添加单击事件监听
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//通过for循环遍历单选按钮组
String like=""; //保存选中的值
if(like1.isChecked()) //当第一个复选按钮被选中
like+=like1.getText().toString()+" ";
if(like2.isChecked()) //当第一个复选按钮被选中
like+=like2.getText().toString()+" ";
if(like3.isChecked()) //当第一个复选按钮被选中
like+=like3.getText().toString()+" ";
Toast.makeText(MainActivity.this, like, Toast.LENGTH_SHORT).show(); //显示被选中的复选按钮
}
});
}
//创建一个状态改变监听对象
private OnCheckedChangeListener checkBox_listener=new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){ //判断复选按钮是否被选中
Log.i("复选按钮","选中了["+buttonView.getText().toString()+"]");
}
}
};
}
显示效果:
3.4图像视图
简介:
图像视图(ImageView),用于在屏幕中显示任何Drawable对象,通常用来显示图片。在Android中,可以使用两种方法向屏幕中添加图像视图,一种是XML,一种是Java,我们推荐使用前者。
在使用ImageView组件显示图像时,通常可以将要显示的图片放置在res/drawable目录中,然后应用不同的代码将其显示在布局管理器中。
基本语法:
<ImageView
属性列表
>
</ImageView>
ImageView支持的XML属性
android:
adjustViewBounds 用于设置ImageView是否调整自己的边界来保持所显示图片的长度比
android:maxHeight 设置ImageView的最大高度,需要设置
android:adjustViewBounds属性值为true,否则不起作用
android:maxWidth 设置ImageView的最大宽度,需要设置
android:adjustViewBounds属性值为true,否则不起作用
android:scaleType 用于设置所显示图片如何缩放或移动以适应ImageView的大小,其属性值有:
matrix(使用matrix方式进行缩放)
fitXY(对图片横向、纵向独立缩放,使得该图片完全适应于该ImageView,图片的纵横比可能会改变)
fitStart(保持纵横比缩放图片,直到该图片能完全显示在ImageView中,缩放完成后该图片放在ImageView的左上角)
fitCenter(保持纵横比缩放图片,直到该图片能完全显示在ImageView中,缩放完成后该图片放在ImageView的中央)
fitEnd(保持纵横比缩放图片,直到该图片能完全显示在ImageView中,缩放完成后该图片放在ImageView的右下角)
center(把图片放置在ImageView的中间,但不进行任何缩放)
centerCrop(保持纵横比缩放图片,以使得图片能够完全覆盖ImageView)
centerInside(保持纵横比缩放图片,以使得ImageView能完全显示该图片)
android:src 用于设置ImageView所显示的Drawable对象的ID,例如:设置显示保存在res/drawable目录下的名称为flower.jpg的图片,可以将属性设置为android:src=”@drawable/flower”
android:tint 用于为图片着色,其属性值可以是#rgb、#argb、#rrggbb或#aarrggbb表示的颜色值
备注:上面给出的只是该组件的部分属性,关于其他属性可以去Android官方给出的API查找
代码实例:
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="horizontal"
android:background="@drawable/background">
<ImageView android:id="@+id/iv1"
android:src="@drawable/flower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<ImageView android:id="@+id/iv2"
android:src="@drawable/flower"
android:maxWidth="180px"
android:maxHeight="180px"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<ImageView android:id="@+id/iv3"
android:src="@drawable/flower"
android:layout_width="180px"
android:layout_height="180px"
android:scaleType="fitEnd"
android:layout_margin="5px"/>
<ImageView android:id="@+id/iv4"
android:src="@drawable/flower"
android:layout_width="180px"
android:layout_height="180px"
android:tint="#77ff0000"
android:layout_margin="5px"/>
</LinearLayout>
显示效果:
3.5列表选择框(下拉框)
简介:
Android中提供的列表选择框(Spinner)相当于网页中常见的下拉列表框,通常用于提供一系列可选择的列表项供用户进行选择,从而方便用户。
在Android中,可以使用两种方式向屏幕中添加列表选择框,一种是通过XML布局文件中使用的<Spinner>标记,另一种是Java文件中,通过new关键字创建,推荐使用前者。
基本语法:
<Spinner
android:prompt=”@string/info”
android:entries=”@array/数组名称”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/ID号”
>
</Spinner>
其中,android:entries为可选属性,用于指定列表项,如果布局文件中不指定该属性,可以在Java代码中通过为其指定适配器的方式指定;android:prompt属性也是可选属性,用于指定列表选择框的标题
说明:
在Android4.0中,采用默认的主题(Theme.Holo)时,设置anddroid:prompt属性看不到具体的效果,如果采用Theme.Black,就可以在弹出的下拉框中显示该标题
通常情况下,如果列表框中要显示的列表项是可知的,那么可将其保存在数组资源文件中,然后通过数组资源来为列表选择框指定列表项,这样就可以在不编写java代码的情况下实现一个列表选择框。
在屏幕上添加完列表框后,可以使用列表框的getSelectedItem()方法获取列表选择框的选中值:
Spinner spinner = (Spinner)findViewById(R.id.spriner1);
sprinner.getSelectedItem();
在添加完列表选择框后,如果需要在用户选择不同的列表项后,执行相应的处理,则可以为该列表项选择框,添加OnItemSelectedListener事件监听器,如:
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long id){
String result = parent.getItemAtPosition(pos).toString() //获取选择项的值
Log.i(”Spinner示例”,result);
}
@Override
public void onNothingSelected(AdapterView<?> arg0){}
});
在使用列表选择框时,如果不在布局文件中直接为其指定要显示的列表项,也可以通过为其指定适配器的方式指定。具体步骤如下:
1. 创建适配器对象,通常使用ArrayAdapter类。
在Android中创建适配器可以使用以下两种方式:
(1) 通过数组资源文件创建
通过数组资源文件创建适配器,需要使用ArrayAdapter类的createFromResource()方法,具体代码如下:
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.ctype,
android.R.layout.simple_dropdown_item_1line);
(2) 通过使用java文件中的字符串数组创建
使用此方式需要先创建一个一维的字符串数组,用于保存要显示的列表项,然后使用ArrayAdapter类的构造方法
ArrayAdapter(Context context, int textViewResourceId, T[] objects)实例化一个ArrayAdapter类的实例,具体代码如下:
String[] ctype = new String[]{”身份证”,”学生证”,”军人证”};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item,ctype);
2. 为适配器设置列表框下拉时的选择样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
3. 将适配器与选择列表关联:
spinner.setAdapter(adapter);
实例代码:
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="horizontal" >
<Spinner android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/ctype"/>
<Spinner android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> adapter, View arg1,
int pos, long arg3) {
String result = adapter.getItemAtPosition(pos).toString();
Log.i("Spinner 示例", result);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//通过适配器来添加下拉列表
String[] items = new String[] {
"男人","女人","动物","水","火"
};
//通过一维数组创建适配器
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, items);
//设置列表下拉框下拉时的选项样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
//将适配器与选择列表框关联
spinner2.setAdapter(adapter);
}
}
显示效果:
3.6列表视图
简介:
列表视图(ListView)是Android中最常用的一种视图组件,它以垂直列表的形式列出需要选择的列表项。如:系统设置、功能内容列表等。在Android中有两种方式可以向屏幕添加列表视图;1.直接使用ListView组件创建;2.让Activity继承ListActivity实现类。
1. 直接使用ListView组件创建
直接使用ListView组件创建,有两种方式,一种是采用XML布局文件中的<ListView>组件,一种是通过java new一个组件的对象,我们推荐使用第一种。
基本语法:
<ListView
属性列表
>
</ListView>
ListView支持的常用的XML属性
android:divider 用于为列表视图设置分隔条,既可以使用颜色分隔,也可以用Drawable资源分隔
android:dividerHeight 设置分隔条的高度
android:entries 通过数组资源为ListView指定列表项
android:footerDividersEnabled 设置是否在footer View之前绘制分隔条,默认值为true,设置为false时表示不绘制。使用该属性时,需要通过ListView组件提供的addFooterView()方法为ListView设置footer View
android:headerDividersEnabled 设置是否在header View之前绘制分隔条,默认值为true,设置为false时表示不绘制。使用该属性时,需要通过ListView组件提供的addHeaderView()方法为ListView设置header View
使用说明:
跟列表选择框一样,设置视图列表的列表项有两种方式:
1. 通过资源文件去添加:
<ListView android:id=”@+id/listView1”
android:entries=”@array/ctype”
android:layout_width=”match_parent”
android:layout_height=”wrap_parent”/>
上面代码使用的是ctype的数组资源,因此需要在res\values目录中创建一个定义数组资源的XML文件array.xml,并在该文件中添加名称为ctype的字符串数组。
<resources>
<string-array name=”ctype”>
…<!—省略了其他项的代码- - >
</string-array>
</resources>
2. 通过适配器去添加
同Spinner列表选择框一样,可以通过为其设置Adapter来指定需要显示的列表项。通过Adapter来为ListView指定要显示的列表项,可分为两个步骤:
第一步:
创建Adapter对象。对于纯文字的列表项,通常使用ArrayAdapter对象。创建ArrayAdapter对象通常可以有两种方式:
(1) 通过数组资源文件创建;
(2) 通过在Java文件中使用字符串数组创建
以上两种方式与Spinner列表选择框中介绍的创建ArrayAdapter的方式基本相同,所不同的就是创建对象时,指定列表项的外观形式不同,主要有如下外观:
sinmple_list_item_1: 每个列表项都是一个普通文本
sinmple_list_item_2: 每个列表项都是一个普通文本(字体略大)
sinmple_list_item_checked: 每个列表项都有一个已选中的列表项
sinmple_list_item_multiple_choise: 每个列表项都是带复选框的文本
sinmple_list_item_single_choise: 每个列表项都是带单选按钮的文本
第二步:
将创建的适配器对象与ListView相关联,可以通过ListView对象的setAdapter()方法实现,具体代码如下:
listView.setAdapter(adapter); //将适配器与ListView关联
为了在点击ListView的各列表项时获取选择项的值,需要为ListView添加OnItemClickListener事件监听器,具体做法如下:
listView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onIitemClick(AdapterView<?> parent, View arg1, int pos, long id){
String result = parent.getItemAtPosition(pos).toString();
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
});
如果想编写一个列表,前面是图片,后面是标题的列表,如下图:就比较麻烦了.
所以,我们通过一个实例来说明这个效果的制作过程
******************第一步:********************************************
编写main.xml文件,定义ListView视图,如下:
<?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" >
<ListView android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"></ListView>
</LinearLayout>
******************第二步:********************************************
其实,这种带图片的列表,每一个条目(每一行)都是一个利用LinearLayout(线性布局管理器—横向布局)进行布局的,LinearLayout中有两个视图组件,第一个是ImageView,第二个是TextView,这两个组件横向排列,所以,第二步就是制作一个列表视图条目的模板,将这个模板单独放在一个XML文件中,并将XML房间放置在res\layout中,具体如下:
编写items.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView android:id="@+id/image"
android:paddingTop="20px"
android:paddingBottom="20px"
android:paddingRight="10px"
android:adjustViewBounds="true"
android:maxWidth="72px"
android:maxHeight="72px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10px"
android:layout_gravity="center"
android:id="@+id/title"/>
</LinearLayout>
******************第三步:********************************************
编写MainActivity.java文件:
package com.caillc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 获取列表视图
ListView listView = (ListView) findViewById(R.id.listView1);
// 定义并初始化保存图片id的数组
int[] imgId = new int[] { R.drawable.img01, R.drawable.img02,
R.drawable.img03, R.drawable.img04, R.drawable.img05,
R.drawable.img06, R.drawable.img07, R.drawable.img08 };
// 定义并初始化保存列表文字放到Map中,并添加到List集合中
String[] title = new String[] { "保密设置", "安全", "系统设置", "上网", "我的文档",
"GPS导航", "我的音乐", "E-mail" };
//创建一个List集合
List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
//通过for循环将图片id和列表项文字放到Map中,并添加到List集合中
for (int i = 0; i < imgId.length; i++) {
Map<String, Object> map = new HashMap<String, Object>(); //实例化map对象
map.put("image", imgId[i]);
map.put("title", title[i]);
items.add(map); //将map添加到list中
}
//创建SimpleAdapter
SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.items,
new String[] { "image", "title" }, new int[] {R.id.image, R.id.title
});
//将适配器与ListView关联
listView.setAdapter(adapter);
}
}
运行效果如下:
注:SimpleAdapter类的构造方法SimleAdapter(Context context, List<? extends Map<String,?>> data, int resource, String[] from, int[] to)中:
参数context用于指定关联SimpleAdapter运行的视图上下文;
参数data用于指定一个基于Map的列表,该列表中每个Map(条目)对应列表中的一行(Map中的image代表的是条目中的图片,title代表的是条目中的文字);
参数resource用于指定条目模板(定义列表项目的视图布局文件)的id(唯一标识);
参数from用于指定data参数中每个条目(Map)的key值所组成的数组(将添加到Map上关联每一条目的列名称的数组);
参数to用于指定每个条目模板(items.xml)中定义的组件的id所组成的数组(与参数from显示列对应的视图id的数组)
此适配器的作用是将List<Map<Stirng,Object>>作为列表视图的数据来源(图片和标题),通过from与to参数,建立数据来源与条目模板之间的一一映射关系,即将List<Map<Stirng,Object>>中每个条目的值赋值给模板中的ImageView和TextView,从而最终产生如上图的效果。
实例代码:
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" >
<ListView android:id="@+id/listView1"
android:entries="@array/ctype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
<ListView android:id="@+id/listView2"
android:divider="@drawable/greendivider"
android:dividerHeight="3px"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item>情景模式</item>
<item>主题模式</item>
<item>手机</item>
<item>程序管理</item>
<item>通话设置</item>
<item>连接功能</item>
</string-array>
</resources>
MainActivity:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//通过适配器来添加下拉列表选择框
ListView lv = (ListView)findViewById(R.id.listView2);
//添加header view视图
lv.addHeaderView(line());
String[] items = new String[]{"床前明月光","疑是地上霜","举头望明月","低头思故乡"};
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, items);
lv.setAdapter(adapter);
//设置footer view
lv.addFooterView(line());
//添加监听
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
String result = parent.getItemAtPosition(pos).toString();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
});
}
public View line() {
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.line1);
return iv;
}
}
显示效果:
2. 继承ListActivity实现类
如果程序的窗口仅仅需要显示一个列表,则可以直接让Activity继承ListActivity来实现。继承了ListActivity的类中,无需调用setContentView()方法来显示页面,而是可以直接为其设置适配器,从而显示一个列表
在onCreate方法中创建作为列表项的Adapter,并使用setListAdapter()方法将其添加到列表中。
public class MainActivity extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/************创建用于为ListView指定列表项的适配器************/
String[] ctype = new String[]{”情景模式”,”主题模式”,”手机”,”程序管理”};
ArrayAadpter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,ctype);
setListAdapter(adapter);
}
}
为了在单击ListView的各列表项时获取选项的值,需要重新重写父类中的onListItemClick()方法
@Override
protected void onListItemClick(ListView l,View v, int position, long id){
super.onListItemClick(I,v,position,id);
String result = l.getItemAtPosition(position).toString(); //获取选择项的值
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
3.7日期、时间拾取器
简介:
为了让用户能够选择日期和时间,Android提供了日期、时间拾取器,分别是DatePicker组件和TimePicker组件,这两个组件使用比较简单。为了可以在程序中获取用户选择的日期、时间,还需要为DatePicker和TimePicker组件添加事件监听器。其中,DatePicker组件对应的事件监听器是OnDateChangedListener而TimePicker对应的事件监听器是OnTimeChangedListener。
基本语法:
<DatePicker android:id=”@+id/datePicker1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
<TimePicker android:id=”@+id/datePicker1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
在主活动中MainActivity的onCreate方法中,获取日期拾取器和时间拾取器组件,并将时间拾取器组件设置为24小时制式显示:
DatePicker datePicker = (DatePicker)findVidwById(R.id.datePicker1);
TimePickerPicker time = (TimePicker)findVidwById(R.id.datePicker1);
time.setIs24HourView(true);
初始化日期拾取器组件,同时为其设置OnDateChangedListener事件监听器,以及为时间拾取组件添加事件监听器:
datePicker.init(year,month,day,new OnDateChangedListener(){
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute){
MainActivity.this.year = year; //year为当前年份
MainActivity.this.month = month; //year为当前年份
MainActivity.this.day = day; //year为当前年份
show(year,month,day,hour,minute); //通过消息框显示日期和时间
}
timepicker.setOnTimeChangeListener(new OnTimeChangedListener(){
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
MainActivity.this.hour = hourOfDay; //改变hour属性的值
MainActivity.this.minute=minute;
show(year,month,day,hourOfDay,minute);
}
});
});
注:通过DatePicker对象获取的月份是0~11月,而不是1-12月,所以需要将获取的结果加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="horizontal" >
<DatePicker android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TimePicker android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.caillc;
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private int year; // 年
private int month;
private int day;
private int hour;
private int minute;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1); // 获取日期拾取组件
TimePicker timepicker = (TimePicker) findViewById(R.id.timePicker1); // 获取时间拾取组件
timepicker.setIs24HourView(true);
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR); // 获取当前年份
month = calendar.get(Calendar.MONTH); // 获取当前月份
day = calendar.get(Calendar.DAY_OF_MONTH); // 获取当前日
hour = calendar.get(Calendar.HOUR_OF_DAY); // 获取当前小时数
minute = calendar.get(Calendar.MINUTE); // 获取当前分钟数
timepicker.setCurrentHour(hour); //设置当前的小时数(后添加的)
timepicker.setCurrentMinute(minute); //设置当前的分钟数(后添加的)
// 初始化日期拾取器,并在初始化时指定监听器
datepicker.init(year, month, day, new OnDateChangedListener() {
public void onDateChanged(DatePicker arg0, int year, int month,
int day) {
MainActivity.this.year = year; // 改变year属性的值
MainActivity.this.month = month; // 改变month属性的值
MainActivity.this.day = day; // 改变day属性的值
show(year, month, day, hour, minute); // 通过消息框显示日期时间
}
});
// 为时间拾取器设置监听器
timepicker.setOnTimeChangedListener(new OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
MainActivity.this.hour = hourOfDay; // 改变hour属性的值
MainActivity.this.minute = minute; // 改变minute属性的值
show(year, month, day, hourOfDay, minute); // 通过消息框显示选择的日期时间
}
});
}
private void show(int year, int month, int day, int hour, int minute) {
String str = year + "年" + (month+1) + "月" + day + "日 " + hour + ":"
+ minute; // 获取拾取器设置的日期和时间
Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); // 显示消息提示框
}
}
显示效果:
3.8计时器
简介:
计时器(Chronometer)组件可显示从某个起始时间开始,一共过去了多长时间的文本。由于该组件继承自TextView,所以它以文本的形式显示内容。使用该组件比较简单,通常情况下只需要使用以下5个方法:
1. setBase():用于设置计时器的起始时间
2. setFormat():用于设置显示时间的格式
3. start():指定开始计时
4. stop():指定停止计时
5. setOnChronometerTickListener():为计时器绑定事件监听器,当计时器改变时触发该监听器。
基本语法:
<Chronomeer
android:text=”Chronometer”
android:id=”@+id/chronometer1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
在主活动MainActivity的onCreate方法中获取计时器组件,并设置起始时间和显示时间格式、开启计时器,以及为其添加监听器
final Chronometer ch =
(Chronometer)findViewById(R.id.chronometer1); //获取计时器组件
ch.setBase(SsytemClock.elapsedRealtime());
ch.setFormat(”已用时间:%s”); //设置显示时间格式
//添加监听事件
ch.setOnChronometerTickListener(new OnChronometerTickListener(){
@Override
public void onChronometerTick(Chronometer chronometer) {
if(SystemClock.elapsedRealtime()-ch.getBase()>=10000){
ch.stop(); //停止时间
}
}
});
示例代码:
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" >
<Chronometer
android:text="Chronometer"
android:id="@+id/chronometer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java:
package com.caillc;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Chronometer c = (Chronometer)findViewById(R.id.chronometer1);
c.setBase(SystemClock.elapsedRealtime()); //设置起始时间
c.setFormat("已用时间: %s"); //设置显示时间的格式
c.start(); //开启计时器
c.setOnChronometerTickListener(new OnChronometerTickListener(){
public void onChronometerTick(Chronometer view) {
if(SystemClock.elapsedRealtime()-c.getBase() >= 10000) {
c.stop(); //停止计时器
}
}
});
}
}
显示效果:
相关推荐
第一行代码 android学习笔记 修改完整版
Android学习笔记 Android学习笔记 Android学习笔记
Android学习笔记
android 学习笔记(全全整理),吧android学习笔记全部整理下来了,可以去好好学习学习
Android学习笔记(一)——创建第一个Android项目 Android学习笔记(二)android studio基本控件及布局(实现图片查看器) Android学习笔记(三)android studio中CheckBox自定义样式(更换复选框左侧的勾选图像) ...
android 学习笔记 android 学习笔记 android 学习笔记
Android 开发学习笔记,学习android的好资料
自己整理的Android学习笔记 都是一些小细节,有用
学习java和android的笔记 更容易学习android 资深学习android 具有大量学习的笔记 很多的错误 都是从细节开始学习的
Mono for Android学习笔记(一) 搭建Mono for Android开发环境及Hello World
Android学习笔记,介绍怎么学习android。
Android学习笔记(十三) 碎片 完整源码
Android学习笔记(1)-永远不变的Hello World Google的Android SDK发布也有一段时间了,一直想研究一下却苦于找不到时间。利用这个周未,开始强迫自己再次进入学习状态,原因很简单:我看好开放的gPhone。 SDK的下载...
android学习笔记
Android开发学习笔记(整理),整理论坛android学习笔记,较完整的讲解了android的内容。包括:view、activity、service、intent、广播机制、http连接、数据sqllite存储、后台线程、各种layout、偏好、本地文件操作、...
android入门学习笔记
Android基础学习笔记,自己总结,适合初学者。
Android_学习笔记 Android_学习笔记 Android_学习笔记