苏icp备09048854号8011701号是哪间公司备案号?

Android4开发入门经典 之 第四部分:用户界面
- jenmhdn - ITeye技术网站
博客分类:
常用Widgets和布局
常用的Widgets
TextView、Button、 EditText、CheckBox、RadioGroup、RadioButton、Spinner、ProgressBar、ImageView、Date Picker、Time Picker、Rating Bar、Gallery 、Auto Complete ……
常用的Layout
Linear Layout、Relative Layout、Table Layout、Grid View、Tab Layout、List View
Android常用的Widgets的属性大同小异,这里以TextView为例来看一看
1:android:autoLink
设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接。可选值(none/web/email/phone/map/all)
2:android:autoText
如果设置,将自动执行输入值的拼写纠正。此处无效果,在显示输入法并输入的时候起作用。
3:android:bufferType
指定getText()方式取得的文本类别。选项editable 类似于StringBuilder可追加字符,也就是说可调用append方法设置文本内容。spannable 则可在给定的字符区域使用样式,例如:
java代码:
TextView tv = new TextView(this);
SpannableStringBuilder style=new SpannableStringBuilder("这是测试111");
style.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.YELLOW), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(style);
4:android:capitalize
设置英文字母大写类型。此处无效果,需要弹出输入法才能看得到,参见EditView此属性说明。
5:android:cursorVisible
设定光标为显示/隐藏,默认显示。
6:android:digits
设置允许输入哪些字符。如“.+-*/% ()”
7:android:drawableBottom
在text的下方输出一个drawable,如图片。如果指定一个颜色的话会把text的背景设为该颜色,并且同时和background使用时覆盖后者。
8:android:drawableLeft
在text的左边输出一个drawable,如图片。
9:android:drawablePadding
设置text与drawable(图片)的间隔,与drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。
10:android:drawableRight
在text的右边输出一个drawable。
11:android:drawableTop
在text的正上方输出一个drawable。
12:android:editable
设置是否可编辑。
13:android:editorExtras
设置文本的额外的输入数据。
14:android:ellipsize
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—-省略前面显示不了的数据;”end” ——省略后面显示不了的数据;”middle”—-省略中间显示不了的数据;”marquee” ——以跑马灯的方式显示(动画横向移动)
15:android:freezesText
设置保存文本的内容以及光标的位置。
16:android:gravity
设置文本位置,如设置成“center”,文本将居中显示。
17:android:hintText
为空时显示的文字提示信息,可通过textColorHint设置提示信息的颜色。此属性在 EditView中使用,但是这里也可以用。
18:android:imeOptions
附加功能,设置右下角IME动作与编辑框相关的动作,如actionDone右下角将显示一个“完成”,而不设置默认是一个回车符号。这个在EditView中使用,此处无用。
19:android:imeActionId
设置IME动作ID。
20:android:imeActionLabel
设置IME动作标签。
21:android:includeFontPadding
设置文本是否包含顶部和底部额外空白,默认为true。
22:android:inputMethod
为文本指定输入法,需要完全限定名(完整的包名)。例如:com.google.android.inputmethod.pinyin。
23:android:inputType
设置文本的类型,用于帮助输入法显示合适的键盘类型。在EditView中使用,这里无效果。
24:android:linksClickable
设置链接是否点击连接,即使设置了autoLink。
25:android:marqueeRepeatLimit
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为 marquee_forever时表示无限次。设置跑马灯的效果
android:singleLine="true"
android:focusable="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
26:android:ems
设置TextView的宽度为N个字符的宽度。这里测试为一个汉字字符宽度
27:android:maxEms
设置TextView的宽度为最长为N个字符的宽度。与ems同时使用时覆盖ems选项。
28:android:minEms
设置TextView的宽度为最短为N个字符的宽度。与ems同时使用时覆盖ems选项。
29:android:maxLength
限制显示的文本长度,超出部分不显示。
30:android:lines
设置文本的行数,设置两行就显示两行,即使第二行没有数据。
31:android:maxLines
设置文本的最大显示行数,与width或者layout_width结合使用,超出部分自动换行,超出行数将不显示。
32:android:minLines
设置文本的最小行数,与lines类似。
33:android:lineSpacingExtra
设置行间距。
34:android:lineSpacingMultiplier
设置行间距的倍数。如”1.2”
35:android:numeric
如果被设置,该TextView有一个数字输入法。此处无用,设置后唯一效果是TextView有点击效果,此属性在EdtiView将详细说明。
36:android:password
以小点”.”显示文本
37:android:phoneNumber
设置为电话号码的输入方式。
38:android:privateImeOptions
设置输入法选项,此处无用,在EditText将进一步讨论。
39:android:scrollHorizontally
设置文本超出TextView的宽度的情况下,是否出现横拉条。
40:android:selectAllOnFocus、
如果文本是可选择的,让他获取焦点而不是将光标移动为文本的开始位置或者末尾位置。 TextView中设置后无效果。
41:android:shadowColor
指定文本阴影的颜色,需要与shadowRadius一起使用。
41:android:shadowDx
设置阴影横向坐标开始位置。
42:android:shadowDy
设置阴影纵向坐标开始位置。
43:android:shadowRadius
设置阴影的半径。设置为0.1就变成字体的颜色了,一般设置为3.0的效果比较好。
44:android:singleLine
设置单行显示。如果和layout_width一起使用,当文本不能全部显示时,后面用“…”来表示。如android:text=“test_ singleLine “,android:singleLine=”true” android:layout_width=“20dp”将只显示“t…”。如果不设置singleLine或者设置为false,文本将自动换行
45:android:text
设置显示文本.
46:android:textAppearance
设置文字外观。如 “?android:attr/textAppearanceLargeInverse”这里引用的是系统自带的一个外观,?表示系统是否有这种外观,否则使用默认的外观。可设置的值如下:textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse
47:android:textColor
设置文本颜色
48:android:textColorHighlight
被选中文字的底色,默认为蓝色
49:android:textColorHint
设置提示信息文字的颜色,默认为灰色。与hint一起使用。
50:android:textColorLink
文字链接的颜色.
51:android:textScaleX
设置文字之间间隔,默认为1.0f。
52:android:textSize
设置文字大小,推荐度量单位”sp”,如”15sp”
53:android:textStyle
设置字形[bold(粗体) 0, italic(斜体) 1, bolditalic(又粗又斜) 2] 可以设置一个或多个,用“|”隔开
54:android:typeface
设置文本字体,必须是以下常量值之一:normal 0, sans 1, serif 2, monospace(等宽字体) 3]
55:android:height
设置文本区域的高度,支持度量单位:px(像素)/dp/sp/in/mm(毫米)
56:android:maxHeight
设置文本区域的最大高度
57:android:minHeight
设置文本区域的最小高度
58:android:width
设置文本区域的宽度,支持度量单位:px(像素)/dp/sp/in/mm(毫米),与layout_width 的区别看这里。
59:android:maxWidth
设置文本区域的最大宽度
60:android:minWidth
设置文本区域的最小宽度
正常Button,同以前使用一样。只不过注册事件的时候,还可以在布局文件里面直接注册事件处理,形如:android:OnClick=“testMethod”,那么运行的时候,会调用public void testMethod(View view){ }的方法
Small的Button
只需要在设置中添加style= “?android:attr/buttonStyleSmall“ ,就可以得到比正常情况小的Button
ToggleButton,就是点击,状态会交替出现的Button
java代码:
&ToggleButton
android:textOn="设置选定状态的显示文本"
android:textOff="设置未选状态的显示文本"
在程序里面可以通过isChecked方法来判断当前是否选中的状态
java代码:
ImageButton
&ImageButton
android:src="@drawable/myimg" ......
限制输入框内容的属性,在layout中配置信息
1:android:digits=".+-*/%()“
限制输入框中只能输入自己定义的这些字符串 如果输入其它将不予以显示
2:android:phoneNumber="true“ 限制输入框中只能输入手机号码
3:android:password="true“限制输入框中输入的任何内容将以“*”符号来显示
4:android:hint=“默认文字“ 输入内容前默认显示在输入框中的提示文字
5:android:textColorHint="#FF0000“设置文字内容颜色
6:android:enabled="false“设置输入框不能被编辑
7:android:maxLength=“10“ 控制输入的最大长度
8:android:numeric 控制输入的数字类型
selectAll()方法是内容全选,getSelectionStart()和getSelectionEnd()可以用于获取选中的内容
可以通过设置EditText的高度来模拟TextArea
Checkbox、RadioGroup和RadioButton
配置的示例如下
java代码:
&RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:checkedButton="@+id/mycolor"
android:id="@+id/menu"&
&RadioButton
android:text="red"
android:id="@+id/red"
&RadioButton
android:text="green"
android:id="@+id/green" /&
&RadioButton
android:text="blue"
android:id="@+id/blue" /&
android:text="请选择颜色"
android:id="@+id/choice" /&
&/RadioGroup&
配置的示例如下
java代码:
&Spinner android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:prompt="@string/select_color"
准备数据,如果是固定的数据,可以在string下面新建一个arrays.xml,内容配置的示例如下:
java代码:
&resources&
&string-array name="colors"&
&item&red&/item&
&item&green&/item&
&item&blue&/item&
&/string-array&
&/resources&
在程序里面把数据设置到Spinner,示例代码如下:
java代码:
final Spinner sp = (Spinner)this.findViewById(R.id.spinner);
ArrayAdapter&CharSequence& adapter = ArrayAdapter.createFromResource(
this, R.array.colors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter);
为Spinner设置事件处理,调用setOnItemSelectedListener方法来设置
要指定默认的选项,可以使用setSelection方法
如果是动态的数据,可以不用定义arrys.xml,而完全用程序设置,示例代码:
java代码:
ArrayAdapter&CharSequence& adapter = new ArrayAdapter&CharSequence&(this,android.R.layout.simple_spinner_item);
adapter.add("red");
adapter.add("blue");
如果要动态的修改数据,可以使用ArrayAdapter的add和remove方法,注意:固定数据是不可以修改的,也就是不支持这些方法
在事件处理里面,会直接传入被选中项的位置,也就是索引
如果要取得被选中项的文本,可以使用如下代码:
java代码:
sp.getSelectedItem().toString()
ProgressBar
基本的进度条,直接在布局文件里面配置,示例如下:
java代码:
&ProgressBar
android:id="@+id/progress_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:max="100"
android:progress="10"
android:secondaryProgress="20" /&
可以通过设置incrementProgressBy()方法来增加进度,incrementProgressBy()方法来减少进度;当然对应的也有incrementSecondaryProgressBy()方法和incrementSecondaryProgressBy()方法。 增加设置正数,减少设置负数。
当然也可以直接使用setProgress和setSecondaryProgress方法来改变进度
可以通过修改style来显示不同的进度样式,具体的Style可以从文档得到
主要是在布局文件里面配置,常见属性如下:
1:src:设置要展示的图片
2:adjustViewBounds:调整边框时是否保持可绘制对象的宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果。比如想设置图片固定大小,又想保持图片宽高比,需要如下设置:
1) 设置setAdjustViewBounds为true;
2) 设置maxWidth、MaxHeight;
3) 设置设置layout_width和layout_height为wrap_content。
3:scaleType:设置图片的填充方式。 有如下可取的值:
1)matrix :用矩阵来绘图
2)fitXY:拉伸图片(不按比例)以填充View的宽高
3)fitStart :按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的左边
4)fitCenter :按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的中间
5)fitEnd :按比例拉伸图片,拉伸后图片的高度为View的高度,且显示在View的右边
6)center :按原图大小显示图片,但图片宽高大于View的宽高时,截图图片中间部分显示
7)centerCrop :按比例放大原图直至等于某边View的宽高显示。
8)centerInside
:当原图宽高或等于View的宽高时,按原图大小居中显示;反之将原图缩放至View的宽高居中显示。
DatePicker
DatePicker组件通常是通过Dialog来显示的,所以在需要DatePicker的地方,需要调用显示Dialog的方法,也就是showDialog(int)方法
在创建Dialog的回调方法中,去真正创建DatePickerDialog,示例代码如下:
java代码:
protected Dialog onCreateDialog(int id) {
switch (id) {
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
return null;
注意:上面代码里面的mDateSetListener为OnDateSetListener的实现,在里面处理日期选择后的后续处理。
TimePicker
TimePicker的使用和DatePicker的使用大同小异,也是在需要TimePicker的地方,通常需要调用显示Dialog的方法,也就是showDialog(int)方法
在创建Dialog的回调方法中,去真正创建TimePickerDialog,示例代码如下:
java代码:
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
return null;
1:上面代码里面的mTimeSetListener为OnTimeSetListener的实现,在里面处理日期选择后的后续处理。
2:TimePicker只有时和分,没有秒,后面一个boolean参数表示是否24小时
RatingBar的使用非常简单,在布局文件里面,定义好RatingBar,然后在代码里面,就可以通过实现OnRatingBarChangeListener,来获取评分的结果了。
常用属性如下:
1:isIndicator:是否是一个指示器(用户无法进行更改)
2:numStars:显示的星型数量,是一个整数值
3:rating:默认的评分,是浮点类型
4:stepSize:评分的步长,是浮点类型,通常设置成“1.0”
5:style:这个要注意,前面不加“android:”,直接使用style,当然可以不设置,使用默认的风格,如果要设置风格,常用的风格示例如下:
1)style="?android:attr/ratingBarStyleIndicator“
2)style="?android:attr/ratingBarStyleSmall“
3)style="?android:attr/ratingBarStyle"
Gallery的使用相对要麻烦一点,首先在布局文件里面要定义
然后在程序里面,需要对Gallery赋值,也就是设置要展示的图片,这就需要自定义一个Adapter,来把图片的数据适配成为Gallery需要的数据,示例如下:
java代码:
class ImageAdapter extends BaseAdapter {
private Context mC
private Integer[] mImageIds = {R.drawable.icon,R.drawable.icon,
R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon,
R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon};
public ImageAdapter(Context c) {
mContext =
public int getCount() { return mImageIds.
public Object getItem(int position) { return }
public long getItemId(int position) { return }
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageV
然后在程序里面,对Gallery设置Adapter,当然就是刚才实现的自定义的Adapter实例了。
Gallery的事件监听器是:OnItemClickListener,通过它可以实现事件处理
如果要给Gallery设置风格的话,需要在res/values文件夹下面创建一个attrs.xml,示例代码如下:
java代码:
&?xml version="1.0" encoding="utf-8"?&
&resources&
&declare-styleable name="MyGallery"&
&attr name="android:galleryItemBackground" /&
&/declare-styleable&
&/resources&
然后在自定义的适配器里面,在构造方法里面,添加如下的代码:
java代码:
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.MyGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.MyGallery_android_galleryItemBackground, 0);
有了风格过后,在创建ImageView的时候,就可以使用这个风格了,在getView方法里面添加如下代码:
java代码:
imageView.setBackgroundResource(mGalleryItemBackground);
这样每个ImageView就都会有一个相同的背景了。
AutoComplete
AutoComplete的使用比较简单,首先在布局文件里面配置使用AutoCompleteTextView
然后在程序里面,为它设置值,示例代码如下:
java代码:
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
ArrayAdapter&String& adapter = new ArrayAdapter&String&(this, R.layout.autoc_item, new String[]{"abc","abd","abcd","bcd","bef"});
textView.setAdapter(adapter);
由于组件在显示提示数据的时候,是采用的列表的形式,因此需要去为list中的每个项定义一个布局文件,其实非常简单,用TextView即可,比如,在layout文件夹里面添加一个auto_item.xml,里面配置示例如下:
java代码:
&TextView xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#ff0000"&
&/TextView&
当然也可使用系统提供的布局,比如:android.R.layout. simple_dropdown_item_1line
上面的示例是输入单个值的,要想一次输入多个值,而且每个都想要有自动完成的功能该怎么办呢?可以使用MultiAutoCompleteTextView
首先在布局文件设置使用MultiAutoCompleteTextView,示例代码如下:
java代码:
&MultiAutoCompleteTextView android:id="@+id/autocomplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
然后在程序中,为他设置值,示例代码如下:
java代码:
MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.autocomplete);
ArrayAdapter&String& adapter = new ArrayAdapter&String&(this, R.layout.autoc_item, new String[]{"abc","abd","abcd","bcd","bef"});
textView.setAdapter(adapter);
textView.setTokenizer(new maTokenizer());
最后一句是设置多个值之间用什么分割,不可少。
LinearLayout
java代码:
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"&
android:text="red"
android:gravity="center_horizontal"
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/&
android:text="green"
android:gravity="center_horizontal"
android:background="#00ff00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/&
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000ff"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/&
java代码:
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/&
&/LinearLayout&
&LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"&
android:text="第一行"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/&
android:text="第二行"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/&
android:text="第三行"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/&
&/LinearLayout&
&/LinearLayout&
RelativeLayout
java代码:
&RelativeLayout xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入:"/&
android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label"/&
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="确定" /&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="取消" /&
&/RelativeLayout&
TableLayout
java代码:
&TableLayout xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"&
&TableRow&
&TextView android:layout_column="1" android:text="张三" android:padding="3dip" /&
android:text="25岁" android:gravity="right" android:padding="3dip" /&
&/TableRow&
&TableRow&
&TextView android:layout_column="1" android:text="李四" android:padding="3dip" /&
android:text="22岁"
android:gravity="right"
android:padding="3dip" /&
&/TableRow&
android:layout_height="2dip" android:background="#FF909090" /&
&TableRow&
android:text="王五"
android:padding="3dip" /&
android:text="本科毕业"
android:padding="3dip" /&
android:text="23岁"
android:gravity="right"
android:padding="3dip" /&
&/TableRow&
&/TableLayout&
TableLayout的常用属性如下:
android:stretchColumns :拉伸指定的列来填充一行的空白
android:collapseColumns :隐藏指定的列,隐藏多列用逗号分开,如“1,2”
android:shrinkColumns :收缩指定的列
android:layout_column :widget在一行里面所在的column的索引
android:layout_span :widget跨越的列数,也就是合并的列数
首先在布局文件里面配置使用GridView,示例代码如下:
java代码:
&GridView xmlns:android="/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="50dp"
android:numColumns="5"
android:verticalSpacing="10dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center"
然后在程序里面,为它设置值,示例代码如下:
java代码:
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
然后在程序里面,为它设置值,需要使用一个自定义的Adapter,示例代码如下:
java代码:
class ImageAdapter extends BaseAdapter {
private Context mC
public ImageAdapter(Context c) { mContext =
public int getCount() {return mThumbIds. }
public Object getItem(int position) { return null;}
public long getItemId(int position) { return 0;
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageV
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(0, 0,0,0);
imageView.setBackgroundColor(Color.RED);
imageView = (ImageView) convertV
imageView.setImageResource(mThumbIds[position]);
return imageV
private Integer[] mThumbIds = {
R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon
事件处理是实现OnItemClickListener,示例代码如下:
java代码:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView&?& parent, View v,
int position, long id) {
Toast.makeText(TestWidgets.this, "" + position,
Toast.LENGTH_SHORT).show();
要实现基本的TabLayout,首先需要在布局文件里面定义,示例代码如下:
java代码:
&TabHost xmlns:android="/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
&LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"&
&TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /&
&FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" /&
&/LinearLayout&
&/TabHost&nTabHost就是用来存放多个Tab的容器
TabWidget就是实现Tab的Widget
FrameLayout是Tab的内容,FrameLayout是Android最简单的布局对象。它被定制为屏幕上的一个空白区域,可以在其中填充一个单一对象 ,所有的子元素将会固定在左上角;不能为FrameLayout中的子元素指定位置。后一个子元素将会直接覆盖前一个子元素。
TabHost必须设置为@android:id/tabhost,TabWidget必须设置android:id为@android:id/tabs,FrameLayout需要设置android:id为@android:id/tabcontent
有了布局过后,就可以直接在程序里面设置Tab了,示例如下:
1:不再继承Activity,而是继承TabActivity,对于Tab布局的实现,现在已经不推荐使用了,建议改用Fragment来实现,但为了学习这个知识,还是沿用。
2:得到TabHost,示例代码如下:
java代码:
TabHost tabHost = getTabHost();
3:创建一个Intent对象,做为点击tab后的事件响应,示例代码如下:
java代码:
Intent intent = new Intent().setClass(this, MyActivity.class);
intent.putExtra("showStr", "第一个tab");
Intent设置的Class就是点击tab,内容页显示出来的Activity。
4:创建一个TabSpec,也就是一个Tab的描述,包含一个tab加上下面的显示内容。简单点说,TabSpec就是一个完整的Tab页。示例代码如下:TabHost.TabSpec spec = tabHost.newTabSpec("FirstTag")
.setIndicator("第一个Tab").setContent(intent);
5:然后把这个TabSpec添加到TabHost中即可,示例代码如下:
java代码:
tabHost.addTab(spec);
6:如法炮制,就可以添加任意多个Tab了。
如果要设置默认选中的Tab,可以使用TabHost的setCurrentTab方法
如果想要给Tab设置图片,方法步骤如下:
1:为每个Tab准备好两张图片,一张是选中时使用,一张是未选中时使用
2:在res/drawable/ 下面,为每个tab增加一个选择器文件,用来告诉tab如何选择使用这些图片,比如my_tab1_selector.xml、 my_tab2_selector.xml,示例代码如下:
java代码:
&?xml version="1.0" encoding="utf-8"?&
&selector xmlns:android="/apk/res/android"&
&item android:drawable="@drawable/icon2"
android:state_selected="true" /&
&item android:drawable="@drawable/icon" /&
&/selector&
3:在程序中,得到Resources对象,可以通过它来获取资源,示例代码如下:
java代码:
Resources res = getResources();
4:在创建TabSpec的时候,在设置每个TabSpec的Indicator的时候,设置这个Indicator使用的图片资源,示例代码如下:
java代码:
spec = tabHost.newTabSpec("FirstTag").setIndicator(
"第一个Tab",res.getDrawable(R.drawable.my_tab1_selector))
.setContent(intent);
5:如法炮制,就可以为每个Tab设置图标了。
对于最基本的ListView,直接在代码里面写,示例如下:
java代码:
public class TestList extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter&String&(this,
android.R.layout.simple_list_item_1, new String[]{"111","222","333"}));
protected void onListItemClick(ListView l,View v,int position,long id){
super.onListItemClick(l, v, position, id);
String s = ""+l.getItemAtPosition(position);
System.out.println("now click="+s);
List的单选模式,示例代码如下:
java代码:
setListAdapter(new ArrayAdapter&String&(this,
android.R.layout. simple_list_item_single_choice, new String[]{"111","222","333}));
this.getListView().setItemsCanFocus(false);
this.getListView().setChoiceMode(ListView. CHOICE_MODE_SINGLE);
List的多选模式,示例代码如下:
java代码:
setListAdapter(new ArrayAdapter&String&(this,
android.R.layout.simple_list_item_multiple_choice, new String[]{"111","222","333}));
this.getListView().setItemsCanFocus(false);
this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
在ListView里面设置多个值的方法
在ListView的main.xml布局文件中
java代码:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:id="@id/android:list"
&/ListView&
&/LinearLayout&
&/LinearLayout&
在ListView的Item的布局文件中
java代码:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
&RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userId"
&/TextView&
java代码:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userName"
android:layout_marginLeft="20dip"
android:layout_toRightOf="@id/userId"
&/TextView&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/userAge"
android:layout_marginLeft="20dip"
android:layout_toRightOf="@id/userName"
&/TextView&
&/RelativeLayout&
&/LinearLayout&
在java程序中
1:要继承ListActivity
2:使用Adapter来为List设置值
java代码:
List list = new ArrayList();
Map map1 = new HashMap();
map1.put("userId","11");
map1.put("userName","11N");
map1.put("age","11");
list.add(map1);
Map map2 = new HashMap();
map2.put("userId","22");
map2.put("userName","22N");
map2.put("age","22");
list.add(map2);
setListAdapter(new SimpleAdapter(this, list,R.layout.list,new String[]{"userId","userName","age"}, new int[]{R.id.userId,R.id.userName,R.id.userAge}));
3:处理item的点击事件
java代码:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
System.out.println("sssssssss===="+l.getItemAtPosition(position));
在Android系统中,菜单分成三种
1:Options Menu :就是当你按MENU键的时候显示的菜单
2:Context Menu :在点击的地方浮动出现的菜单
3:Submenu
创建菜单资源
通常在菜单资源的xml里面,把菜单项都预先定义好。虽然也可以在程序中通过代码添加菜单项,但更推荐使用菜单资源的xml。
1:在res/menu/ 下创建菜单的xml文件,示例代码如下:
java代码:
&menu xmlns:android="/apk/res/android"&
&item android:id="@+id/to_add"
android:icon="@drawable/icon"
android:title="转向新增" /&
&item android:id="@+id/to_update"
android:icon="@drawable/icon"
android:title="转向修改" /&
2:在代码里面使用菜单资源文件,示例如下:
java代码:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test_menu, menu);
return true;
响应菜单,在程序里面,需要写代码来实现菜单被点击后要实现的功能
java代码:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.to_add:
Log.i("javass","now to add"); break;
case R.id.to_update:
Log.i("javass","now to update"); break;
default: return super.onOptionsItemSelected(item);
return true;
Option Menu一般最多显示六个,如果更多的话,会出现一个更多的按钮。
Android3.0以后,可以直接在菜单描述文件里面配置android:onClick,指定当菜单被点击时要调用的方法,该方法接受一个MenuItem参数。示例如下:
java代码:
public void myToAdd(MenuItem item) {
Log.i("javass","now in myToAdd");
当配置android:onClick后,上面的onOptionsItemSelected里面对应的菜单处理就不会被调用了
上下文菜单
上下文菜单就相当于PC上的右击,创建上下文菜单:
1:菜单项同样来源于菜单资源文件
2:为需要上下文菜单的view进行注册,示例如下:
java代码:
this.registerForContextMenu(btnRun);
3:覆写onCreateContextMenu方法,示例如下:
java代码:
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo info) {
super.onCreateContextMenu(menu, v, info);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test_menu, menu);
4:处理菜单事件,示例如下:
java代码:
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id. new_file:
Log.i("onContextItemSelected", "now ruuuuuuuuuuu");
return true;
default: return super.onContextItemSelected(item);
创建子菜单非常简单,只需要在菜单资源文件中配置即可,示例如下:
java代码:
&item android:id="@+id/new_file"
android:icon="@drawable/ic_launcher"
android:title="@string/new_file"
&item android:id="@+id/create_new"
android:title="now new" /&
&item android:id="@+id/open"
android:title="now open" /&
菜单的事件处理跟前面的做法是一样的。
子菜单可以配合Option菜单或Content菜单使用。
给菜单分组
给菜单分组非常简单,只需要在菜单资源文件中配置即可,示例如下:
java代码:
&group android:id="@+id/group1"&
&item android:id="@+id/create_new"
android:icon="@drawable/ic_launcher"
android:title="now new" /&
&item android:id="@+id/open"
android:icon="@drawable/ic_launcher"
android:title="now open" /&
分组后能干什么
1:可以通过使用setGroupVisible()方法来显示或隐藏一组菜单
2:可以通过使用setGroupEnabled()方法来使一组菜单可用还是不可用
3:可以通过使用setGroupCheckable()方法来使一组菜单可选还是不可选
可选菜单非常简单,只需要在菜单资源文件中配置即可,示例如下:
1:对于单个菜单项,添加android:checkable,即可变成带复选框的菜单
2:对于分组的菜单,直接在Group上添加android:checkableBehavior的配置,它有三个选项:单选、复选、不能选择。
可选菜单一般针对上下文菜单和子菜单
要注意:使用可选菜单的时候,选择的状态必须要通过程序来保存,否则下次进入的时候,是不会保留你选择的状态的。可以在对应的事件处理里面保留选项的状态,然后在菜单重新绘制的时候,重新设置状态。
1:对于Options的子菜单,可以在onPrepareOptionsMenu方法里面设置状态,示例代码如下:
java代码:
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem mi = menu.findItem(R.id.create_new);
mi.setChecked(flag);
return true;
2:对于上下文菜单或其子菜单,可以直接在onCreateContextMenu方法里面设置,因为这个方法每次都会重新执行。
动态修改菜单项
有些时候,需要动态的修改菜单项,该怎么实现呢?
对于Options的菜单及其子菜单
1:覆盖实现onPrepareOptionsMenu方法。
2:在这个方法里面使用menu的方法来维护菜单项
onPrepareOptionsMenu方法和onCreateOptionsMenu方法
1:onCreateOptionsMenu只是在第一次创建菜单的时候调用一次,以后就不调用了
2:onPrepareOptionsMenu方法是每次显示菜单的时候,都要调用一次的方法。
对于上下文的菜单及其子菜单,可以直接在onCreateContextMenu方法里面设置,因为这个方法每次都会重新执行。
编程式实现菜单
前面是声明式实现菜单,也可以采用编程式的方式来实现菜单,常用API为:
2:MenuItem
3:SubMenu,与MenuItem平行
关于Menu的图标
1:子菜单的项不能显示图标
2:上下文菜单和项都不能显示图标
3:带图标的菜单项不能加上复选框
Action Bar基本概念
ActionBar是用来替换传统的title bar的一种新的Widget,通常显示在屏幕的顶端。
通常情况下,Acviton Bar带着应用的Logo放置到最左端,紧接着是应用的title,而其他可选项放置在右边。
在Android3.0以后,所有的Activity都缺省的带着ActionBar了。
1:显示Option Menu中的项,让用户能直接操作
2:提供tab来在多个Fragment之间导航
3:提供drop-down的导航列表
4:提供直接交户的Action Views,比如搜索条等。
使用Action Bar
删除Action Bar
1:Android系统缺省用的是“holographic ” theme,是有Action Bar的,可以在manifext配置文件中配置Activity的时候,指定不需要Action Bar,示例如下:
java代码:
&activity android:theme="@android:style/Theme.Holo.NoActionBar"&
2:也可以在程序中设置,示例如下:
java代码:
ActionBar actionBar = getActionBar();
actionBar.hide();
添加Action Items
一个Action Item就相当于Option Menu中的一个选项。要想把菜单项显示成为Action Item,只需要在菜单项的配置文件中,设置android:showAsAction=“ifRoom” 。也可使用程序设置:
menu.getItem(0).setShowAsAction(MenuItem. SHOW_AS_ACTION_IF_ROOM);
Action Items的事件响应,就是Option Menu的事件响应
添加Action View
可以把其他Action View,也就是Widget添加显示到Action Bar上,典型如搜索条,这里简单的看一下示例:
1:可以通过配置android:actionLayout或者android:actionViewClass 的方式,来实现把widget显示到Action Bar上,比如:
java代码:
&menu xmlns:android="/apk/res/android"&
&item android:id="@+id/menu_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionLayout="@layout/searchview" /&
或者把android:actionLayout=“@layout/searchview” 换成:
android:actionViewClass="android.widget.SearchView"
关于搜索条和更多Widget的使用,将在后面的高级课程里面讲述。
修改Action Bar的风格
1:简单的修改,可以通过:
setBackgroundDrawable()方法:设置Action Bar的背景图
setDisplayUseLogoEnabled()方法:设置可以改变logo,可以通过setLogo方法来设置新的logo。
示例如下:
java代码:
ActionBar actionBar = getActionBar();
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setLogo(R.drawable.app_sample_code);
actionBar.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.app_sample_code));
2:可以通过修改theme的方式,比如:
java代码:
&activity android:name=".ExampleActivity"
android:theme="@android:style/Theme.Holo.Light" /&
什么是Dialog
就是在当前Activity上弹出的小窗口,也就是对话框。Dialog通常是附属于Activity的。
常见的Dialog
1:AlertDialogA dialog :就是弹出提示或警告信息的对话框
2:ProgressDialog : 进度条
3:DatePickerDialog :选择日期的对话框
4:TimePickerDialog :选择时间的对话框
创建和显示Dialog
一般会在onCreateDialog() 方法里面去创建Dialog,如果要在这个方法外创建Dialog,那么需要调用setOwnerActivity(Activity)方法来把Dialog和Activity关联起来。
想要显示Dialog的时候,调用showDialog(int)方法即可。
关闭Dialog
可以在Dialog对象里面使用dismiss()方法,也可以在Activity里面使用dismissDialog(int)方法。
Dismiss listener
如果应用程序想要处理dissmiss dialog的事件,可以定义一个类来实现DialogInterface.OnDismissListener,里面有一个setOnDismissListener()的方法,会在Dialog被销毁的时候触发。
当然,Listener需要被注册,可以使用setOnCancelListener()方法。
实现Dialog风格的Activity
在 AndroidManifest.xml的对应的Activity设置上添加:
android:theme="@android:style/Theme.Dialog"
Alert Dialog
创建带Button的Alert Dialog
java代码:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("确定要删除吗?")
.setCancelable(false)
.setPositiveButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Toast.makeText(HelloWorldActivity.this, “dialog取消", Toast.LENGTH_LONG).show();
.setNegativeButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) { Toast.makeText(HelloWorldActivity.this,“dialog确定", Toast.LENGTH_LONG).show();
AlertDialog alert = builder.create();
其实,可以添加三种按钮,这里添加的是正面和负面,还有一种是中性的,三种按钮的排列顺序是正面、中性、反面。
创建带列表的Alert Dialog
java代码:
final CharSequence[] items = { "红色", "绿色", "蓝色" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择颜色");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
AlertDialog alert = builder.create();
如果想要是单选的列表项,那就不要使用setItems,而用setSingleChoiceItems(),示例如下:
java代码:
final CharSequence[] items = { "红色", "绿色", "蓝色" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择颜色");
builder.setSingleChoiceItems(items, -1,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
dialog.dismiss();
AlertDialog alert = builder.create();
如果想要是多选的列表项,那就不要使用setItems,而用setMultiChoiceItems (),示例如下:
java代码:
inal CharSequence[] items = { "红色", "绿色", "蓝色" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择颜色");
builder.setMultiChoiceItems(items, new boolean[]{false,false,false},new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
builder.setNegativeButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(HelloWorldActivity.this, "dialog确定", Toast.LENGTH_LONG).show();
dialog.dismiss();
AlertDialog alert = builder.create();
Progress Dialog
Progress Dialog扩展自Alert Dialog
如果简单的使用,只需要调用show方法即可,创建就显示出来了:
ProgressDialog dialog = ProgressDialog.show(HelloWorldActivity.this, "", "装载中,请稍候...", true, true);
注意:对于这种方法显示的Dialog,点击界面上其他地方,它就消失了,如果想要它不消失,可以设置setCancelable为false,那通常就需要配合Handler来处理,也就是到了该取消的时候,就由HandlerThread来处理它。示例如下:
1:在外部定义:
java代码:
HandlerThread ht = new HandlerThread("MyThread");
Handler h = null;
2:在启动Dialog的方法里面:
java代码:
if(!ht.isAlive()){
ht.start();
h = new Handler(ht.getLooper()){
public void handleMessage(Message msg) {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
HelloWorldActivity.this.dismissDialog(1);
super.handleMessage(msg);
Message msg = h.obtainMessage();
msg.sendToTarget();
使用Progress Bar,同样需要配合Handler来执行,示例如下:
1:创建Progress Bar,在onCreateDialog方法里面,示例如下:
java代码:
dialog = new ProgressDialog(HelloWorldActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("装载中...");
dialog.setCancelable(false);
dialog.setMax(10);
2:定义Handler和Runnable
java代码:
Handler h = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.arg1 & dialog.getMax()){dialog.setProgress(msg.arg1);}
else{dialog.dismiss();}}};
int num = 0;
Runnable r = new Runnable() {
public void run() {
Message msg = h.obtainMessage();
msg.arg1 = num++;
h.sendMessage(msg);
h.postDelayed(r, 1000L);
3:在显示dialog的同时,要启动Handler的运行,也就是 h.post(r);
也可以通过配置文件来使用Progress Bar,同样需要配合Handler来执行,示例如下:
1:在layout的配置文件里面创建Progress Bar,示例如下:
java代码:
&ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
2:在程序中启动Progress Bar,示例如下:
java代码:
pb = (ProgressBar)HelloWorldActivity.this.findViewById(R.id.progress_bar);
pb.setMax(10);
pb.setVisibility(View.VISIBLE);
h.post(r);
3: Handler和Runnable的实现跟前面类似,这里就不去写了。
Customer Dialog
如果想要使用自己订制的Dialog,那么首先需要定义自己的布局,示例如下:
java代码:
&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" &
&ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp" /&
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
&/LinearLayout&
在程序中获取这些对象,然后设置相应的属性,再显示出来,示例如下:
java代码:
Context mContext = HelloWorldActivity.this;
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.my_dialog);
dialog.setTitle("自己订制的Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("欢迎使用本系统");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
dialog.show();
Handler概述
Handler允许你发送消息到一个线程的消息队列,并处理消息。
Handler和启动它的主线程是运行在同一个线程中的。主线程通过Handler来调度处理消息,变相实现了异步处理。
nHandler应用的地方
1:安排消息或Runnable 在某个主线程中某个地方执行
2:安排一个动作在不同的线程中执行,这个要使用HandlerThread
Handler使用
1:创建Handler对象,实现handleMessage方法
2:创建Runnable对象,在run方法里面进行消息的发送
3:在主线程中启动Handler,使用post方法或其它类似方法
4:在主线程中取消Handler,使用removeCallbacks方法或其它类似方法
示例如下:
java代码:
Handler h = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
int what = 0;
Runnable r = new Runnable() {
public void run() {
Message msg = h.obtainMessage();
msg.obj = "1234567";
msg.what =
h.sendMessage(msg);
h.postDelayed(r, 1000L);
如果想要启动新的线程来处理,使用HandlerThread的示例如下:
1:先定义Handler和HandlerThread:
java代码:
HandlerThread ht = new HandlerThread("MyThread");
Handler h = null;
2:在某个地方(比如某个按钮操作里面)启动HandlerThread:
java代码:
ht.start();
h = new Handler(ht.getLooper()){
public void handleMessage(Message msg) {
super.handleMessage(msg);
Toast.makeText(getApplicationContext(),"msg="+msg.obj,Toast.LENGTH_LONG).show();
3:在某个地方(比如某个按钮操作里面)发送消息:
java代码:
Message msg = h.obtainMessage();
msg.obj="test message";
msg.sendToTarget();
4:在某个地方(比如某个按钮操作里面)可以停止HandlerThread,使用stop方法
Notifications
Android系统中,有如下通知类型:
1:Toast Notification:展示后台的简短消息
2:Status Bar Notification:展示后台对用户的提示,通常需要用户响应
3:Dialog Notification:Activity相关的通知
指定Toast的位置,使用Gravity属性,示例如下:
java代码:
Toast t = Toast.makeText(HelloWorldActivity.this,"okok", Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP|Gravity.LEFT, 5,10);
订制Toast,需订制布局文件,这里使用上一个示例的布局文件来示例,程序写法如下:
java代码:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.my_dialog,
(ViewGroup) findViewById(R.id.layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("欢迎您的到来!");
Toast t = new Toast(HelloWorldActivity.this);
t.setView(layout);
t.setGravity(Gravity.CENTER|Gravity.LEFT, 5,10);
Status Bar Notification :可以由Activity或Service发出。示例如下:
java代码:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.app_sample_
CharSequence tickerText = "请注意";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "重要通知";
CharSequence contentText = "测试";
Intent notificationIntent = new Intent(HelloWorldActivity.this, T2.class);
PendingIntent contentIntent = PendingIntent.getActivity(HelloWorldActivity.this, 0, notificationIntent, 0);
notification.setLatestEventInfo(HelloWorldActivity.this, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1, notification);
NotificationManager 是一个系统级的服务
可以通过NotificationManager 来取消自己的Status Bar Notification,就是使用notity时为通知设置的一个ID,示例如下:
mNotificationManager.cancel(1);//1就是前面notity时传递的id值,必须唯一
为Notification添加提示声音
1:使用系统默认的声音:
notification.defaults |= Notification. DEFAULT_SOUND;
2:使用自己上传的声音:
notification.sound=Uri. parse("file:///data/data/cn.javass/test_cbr.mp3");
为Notification添加提示振动
1:首先要设置权限:&uses-permission android:name= "android.permission.VIBRATE" /&
2:使用系统缺省的振动:notification.defaults |= Notification.DEFAULT_VIBRATE;
3:自己定义振动的参数:
long[] vibrate = {0,100,200,300};
otification.vibrate =
为Notification添加 闪烁 提示,也就是LED灯的闪烁
1:使用系统缺省的:notification.defaults |= Notification.DEFAULT_LIGHTS;
2:自己定义闪烁的参数:
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification. FLAG_SHOW_LIGHTS;
为Notification添加其他特性
1:FLAG_AUTO_CANCEL:当用户点击这个通知后,通知自动消失
2:FLAG_INSISTENT:重复播放音乐直到用户响应
3:FLAG_ONGOING_EVENT:在通知窗口增加Ongoing的标记
4:FLAG_NO_CLEAR:标识通知不能被清除掉
客户化的Notification,只是客户化在通知信息列表里面的展示,其他的跟前面仍然是一样的,示例如下:
1:自己订制一个Layout的配置文件,这里使用前面示例用的Layout
2:不再调用notification.setLatestEventInfo这句话,修改成:
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.my_dialog);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, "重要通知");//这个前面的layout里面没有,可以注掉,也可以自己去layout里面添加上
contentView.setTextViewText(R.id.text, "测试");
//设置在通知信息列表里面显示的视图
otification.contentView = contentV
//设置点击通知信息时触发的事件
otification.contentIntent = contentI
视频配套PPT,视频地址【 】
原创内容 转自请注明【 】
10 顶16 踩
浏览: 96340 次
来自: 成都
你好,这X-gen有源码参考吗
LZ研究过Hibernate4里的监听时间吗?我这边用4.3. ...
看介绍 挺美的!
即这个是综合运用设计模式的
很值得一看

我要回帖

更多关于 苏icp备案查询 的文章

 

随机推荐