Android用户界面
实验目的
1.掌握Android常用控件的使用方法和基本属性。
2.掌握控件的常用事件处理函数的编写方法。
3.掌握Android常用布局的常用属性。
4.掌握线性布局、表格布局和相对布局的使用方法。
实验内容
☆测试Android应用程序中常用控件的使用方法和掌握界面布局的方法
实验步骤
1.创建一个Android应用
2.在界面上拖放对应的控件
3.根据实验资料的提示修改xml文件和java源文件
4.修改界面的布局,查看效果(至少使用两种以上布局实现同样的界面效果)。
5.运行程序,查看效果
要求:每人至少完成5种控件的使用、两种以上布局的使用。
提示:建议先从简单的控件做起,然后逐步过渡到复杂控件,可创建多个应用
以下内容为本人在阅读《疯狂Android讲义》所写的文档,包含各种基础的布局和控件,并且对于其实中的代码进行阅读理解并做出相应的修改,把代码思路以及对某个布局和控件所做出的总结归纳。
布局
线性(LinerLayout)布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity= "right|center_vertical">
<Button android:id="@+id/bn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bn1" />
<Button android:id="@+id/bn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bn2" />
<Button android:id="@+id/bn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bn3" />
<Button android:id="@+id/bn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bn4" />
<Button android:id="@+id/bn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/bn5" /> </LinearLayout>
|
设置参数android:gravity=”right|center_vertical”水平左对齐、垂直居中
表格(TableLayout)布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TableLayout android:id="@+id/TableLayout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:shrinkColumns="1" android:stretchColumns="2"> <Button android:id="@+id/ok1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="独自一行的按钮"/> <TableRow> <Button android:id="@+id/ok2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮"/> <Button android:id="@+id/ok3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="收缩的按钮"/> <Button android:id="@+id/ok4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按钮"/> </TableRow> </TableLayout> <TableLayout android:id="@+id/TableLayout02" android:layout_width="match_parent" android:layout_height="wrap_content" android:collapseColumns="1"> <Button android:id="@+id/ok5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="独自一行的按钮"/> <TableRow> <Button android:id="@+id/ok6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮1"/> <Button android:id="@+id/ok7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮2"/> <Button android:id="@+id/ok8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮3"/> </TableRow> </TableLayout> <TableLayout android:id="@+id/TableLayout03" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1,2"> <Button android:id="@+id/ok9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="独自一行的按钮" /> <TableRow> <Button android:id="@+id/ok10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮" /> <Button android:id="@+id/ok11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按钮" /> <Button android:id="@+id/ok12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按钮" /> </TableRow> <TableRow> <Button android:id="@+id/ok13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按钮" /> <Button android:id="@+id/ok14" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸的按钮" /> </TableRow> </TableLayout> </LinearLayout>
|
直接添加TableRow占用一行第二表格中间的按钮只对指定的按钮设置collapseColumns=‘1’,进行隐藏。
帧(FrameLayout)布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">> <TextView android:id="@+id/view01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="160pt" android:height="160pt" android:background="#f00"/> <TextView android:id="@+id/view02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="140pt" android:height="140pt" android:background="#0f0"/> <TextView android:id="@+id/view03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="120pt" android:height="120pt" android:background="#00f"/> <TextView android:id="@+id/view04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="100pt" android:height="100pt" android:background="#ff0"/> <TextView android:id="@+id/view05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="80pt" android:height="80pt" android:background="#f0f"/> <TextView android:id="@+id/view06" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:width="60pt" android:height="60pt" android:background="#0ff"/> </FrameLayout>
|
使用每0.2秒更换一次任务,每一种颜色设置其对应的pt参数
相对(RelativeLayout)布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/view01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_centerInParent="true"/> <TextView android:id="@+id/view02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_above="@id/view01" android:layout_alignLeft="@id/view01"/> <TextView android:id="@+id/view03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_below="@id/view01" android:layout_alignLeft="@id/view01"/> <TextView android:id="@+id/view04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_toLeftOf="@id/view01" android:layout_alignTop="@id/view01"/> <TextView android:id="@+id/view05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/leaf" android:layout_toRightOf="@id/view01" android:layout_alignTop="@id/view01"/> </RelativeLayout>
|
使用android:layout_xxx控制图片组件的相对位置
网格(GridLayout)布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?xml version="1.0" encoding="utf-8" ?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:rowCount="6" android:columnCount="4" android:id="@+id/root"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:textSize="50sp" android:layout_marginLeft="2pt" android:layout_marginRight="2pt" android:padding="3pt" android:layout_gravity="right" android:background="#eee" android:textColor="#000" android:text="0"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnSpan="4" android:text="清除"/> </GridLayout>
|
定义6*4的组件样式,采用循环向布局添加16个按钮,四其自动填充单元格所有空间
绝对(AbsoluteLayout)布局
使用px,dip,dp,sp,in,mm,pt对坐标进行自定义设置
控件
TextView类
TextView(文本视图)
drawable/bg_border.xml
1 2 3 4 5 6 7
| <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#0000"/> <stroke android:width="4px" android:color="#f00" /> </shape>
|
drawable/bg_border2.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:topLeftRadius="20px" android:topRightRadius="5px" android:bottomRightRadius="20px" android:bottomLeftRadius="5px"/> <stroke android:width="4px" android:color="#f0f" />
<gradient android:startColor="#f00" android:centerColor="#0f0" android:endColor="#00f" android:type="sweep"/> </shape>
|
layout/main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Python" android:textSize="20pt" android:drawableEnd="@drawable/ic_launcher"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:text="机器学习网络爬虫数据分析网络编程GUI" android:ellipsize="middle" android:textAllCaps="true"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:text="Github是https://github.com/zyhang8" android:autoLink="email|phone"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Redis" android:shadowColor="#00f" android:shadowDx="10.0" android:shadowDy="8.0" android:shadowRadius="3.0" android:textColor="#f00" android:textSize="18pt"/> <TextView android:id="@+id/passwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="mima" android:password="true"/> <CheckedTextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="可勾选" android:checkMark="@drawable/ok" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="带边框" android:textSize="24pt" android:background="@drawable/bg_border"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="圆角边框、渐变背景" android:textSize="24pt" android:background="@drawable/bg_border2"/> </LinearLayout>
|
该案例有设置不用颜色、字体、带链接的文本,阴影通过修改shadow参数,,哦,啊苹果修改password参数,勾选文本通过修改checkMark。带圆角边框的文本、渐变背景通过调用background进行渲染
EditText(输入框)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="1"> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户名:" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写登录账号" android:selectAllOnFocus="true"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="密码:" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberPassword"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="年龄:" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生日:" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="date"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="电话号码:" android:textSize="16sp"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写您的电话号码" android:selectAllOnFocus="true" android:inputType="phone"/> </TableRow> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册"/> </TableLayout>
|
通过hint参数指定文本框的提示信息,inputType参数设置各种格式使该文本框只接受某种格式的填写,比如密码只能数字密码,电话只能数字,用户名可以任意字符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="文字带阴影" android:textSize="12pt" android:shadowColor="#aa5" android:shadowRadius="1" android:shadowDx="5" android:shadowDy="5"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/red" android:text="普通按钮" android:textSize="10pt"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_selector" android:textSize="11px" android:text="带图片按钮"/> </LinearLayout>
|
通过指定background属性为按钮增加背景颜色或背景图片吗
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.RadioGroup; import android.widget.TextView;
public class MainActivity extends Activity { RadioGroup rg; TextView show; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rg = (RadioGroup) findViewById(R.id.rg); show = (TextView) findViewById(R.id.show); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { String tip = checkedId == R.id.male ? "您的性别是男人": "您的性别是女人"; show.setText(tip); } }); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性别:"/> <RadioGroup android:id="@+id/rg" android:orientation="horizontal" android:layout_gravity="center_horizontal"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/male" android:text="男" android:checked="true"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/female" android:text="女"/> </RadioGroup> </TableRow> <TableRow> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜欢的颜色:"/> <LinearLayout android:layout_gravity="center_horizontal" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="红色" android:checked="true"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="蓝色"/> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绿色"/> </LinearLayout> </TableRow> <TextView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </TableLayout>
|
通过定义单选钮,并默认勾选第一个单选钮,定义三个复选钮供用户选择喜欢的颜色。另外,设置时间监听器监听单选钮勾选状态的改变
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ToggleButton android:id="@+id/toggle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="横向" android:textOn="纵向" android:checked="true"/> <Switch android:id="@+id/switcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOff="横向" android:textOn="纵向" android:thumb="@drawable/check" android:checked="true"/> <LinearLayout android:id="@+id/test" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="二" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="三" /> </LinearLayout> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.LinearLayout; import android.widget.Switch; import android.widget.ToggleButton;
public class MainActivity extends Activity { ToggleButton toggle; Switch switcher; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); toggle = (ToggleButton)findViewById(R.id.toggle); switcher = (Switch)findViewById(R.id.switcher); final LinearLayout test = (LinearLayout)findViewById(R.id.test); OnCheckedChangeListener listener = new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton button , boolean isChecked) { if(isChecked) { test.setOrientation(1); toggle.setChecked(true); switcher.setChecked(true); } else { test.setOrientation(0); toggle.setChecked(false); switcher.setChecked(false); } } }; toggle.setOnCheckedChangeListener(listener); switcher.setOnCheckedChangeListener(listener); } }
|
默认采用垂直方向的线性布局,通过ToggleButton按钮,Switch按钮绑定监听器,选中状态改变是,布局方向随之改变
AnalogClock和TextClock(时钟)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10pt" android:textColor="#f0f" android:format12Hour="yyyy年MM月dd日 H:mma EEEE" android:drawableEnd="@drawable/ic_launcher"/> <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:dial="@drawable/watch" android:hand_minute="@drawable/hand"/> </LinearLayout>
|
使用Activity显示界面布局,通过textSize,textColor等属性进行控制文本样式
Chronometer(计时器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <Chronometer android:id="@+id/test" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12pt" android:textColor="#ffff0000"/> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动"/> </LinearLayout>
|
使用format属性用于指定计时器的几十个时,绑定时间监听器,但计时器改变是触发该监听器
mageView类
ImageView(图片视图)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center"> <Button android:id="@+id/plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="增大透明度"/> <Button android:id="@+id/minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="降低透明度"/> <Button android:id="@+id/next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一张"/> </LinearLayout> <ImageView android:id="@+id/image1" android:layout_width="wrap_content" android:layout_height="280dp" android:src="@drawable/shuangta" android:scaleType="fitCenter"/> <ImageView android:id="@+id/image2" android:layout_width="120dp" android:layout_height="120dp" android:background="#00f" android:layout_margin="10dp"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| package org.crazyit.ui;
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView;
public class MainActivity extends Activity { int[] images = new int[]{ R.drawable.lijiang, R.drawable.qiao, R.drawable.shuangta, R.drawable.shui, R.drawable.xiangbi, }; int currentImg = 2; private int alpha = 255; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button plus = (Button) findViewById(R.id.plus); final Button minus = (Button) findViewById(R.id.minus); final ImageView image1 = (ImageView) findViewById(R.id.image1); final ImageView image2 = (ImageView) findViewById(R.id.image2); final Button next = (Button) findViewById(R.id.next); next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { image1.setImageResource( images[++currentImg % images.length]); } }); View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { if (v == plus) { alpha += 20; } if (v == minus) { alpha -= 20; } if (alpha >= 255) { alpha = 255; } if (alpha <= 0) { alpha = 0; } image1.setImageAlpha(alpha); } }; plus.setOnClickListener(listener); minus.setOnClickListener(listener); image1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { BitmapDrawable bitmapDrawable = (BitmapDrawable) image1 .getDrawable(); Bitmap bitmap = bitmapDrawable.getBitmap(); System.out.println(bitmap.getWidth()); System.out.println(image1.getWidth()); double scale = 1.0 * bitmap.getHeight() / image1.getHeight(); int x = (int) (event.getX() * scale); int y = (int) (event.getY() * scale); if (x + 120 > bitmap.getWidth()) { x = bitmap.getWidth() - 120; } if (y + 120 > bitmap.getHeight()) { y = bitmap.getHeight() - 120; } image2.setImageBitmap(Bitmap.createBitmap(bitmap , x, y, 120, 120)); image2.setImageAlpha(alpha); return false; } }); } }
|
使用setImageResource()进行动态修改图片样式,下一张则显示数组下一张图片,通过改变Alpha值来调整图片的透明度。通过Bitmap类来截取位图注定部分,返回行位图。但是运行后只有一片蓝屏,不知道哪里出错了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/blue" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/button_selector" /> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10sp" android:layout_gravity="center_horizontal"> <ZoomButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_zoom_down" android:src="@android:drawable/btn_minus" /> <ZoomButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn_zoom_up" android:src="@android:drawable/btn_plus" /> </LinearLayout> <ZoomControls android:id="@+id/zoomControls1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/> </LinearLayout>
|
上面的Button为静态图片,下面的的Button组合了两张图片,点击是切换图片
可以关联到手机中指定的联系人,点击图片会打开相应联系人的联系方式界面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?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"> <QuickContactBadge android:id="@+id/badge" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/purple"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16dp" android:text="zyhang"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.widget.QuickContactBadge;
public class MainActivity extends Activity { QuickContactBadge badge; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); badge = (QuickContactBadge) findViewById(R.id.badge); badge.assignContactFromPhone("18621083837", false); } }
|
通过QuickContactBadge控件与电话号码联系人建立关联,如果手机有号码则建立关联,没有的话新建号码
AdapterView类
ListView(列表视图)&ListActivity
以垂直的形式显示所有列表项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/books" android:divider="#f00" android:dividerHeight="2px" android:headerDividersEnabled="false"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9
| <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="books"> <item>Python自动化运维</item> <item>Python量化投资</item> <item>Pthon数据科学</item> <item>Python数据可视化</item> </string-array> </resources>
|
通过entries指定了列表项数组,通过divider改变列表项之间的分隔条。定义数组资源来使用属性值
Adapter接口及实现
Adapter派生了ListAdapter和SpinnerAdapter子接口,一个为AbsListView提供列表项,一个为AbsSpinnner提供列表项
1 2 3 4 5 6 7 8 9 10
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/myList" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package org.crazyit.ui;
import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView;
public class MainActivity extends Activity { ListView myList; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myList = (ListView) findViewById(R.id.myList); BaseAdapter adapter = new BaseAdapter() { @Override public int getCount() { return 40; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position , View convertView , ViewGroup parent) { LinearLayout line = new LinearLayout(MainActivity.this); line.setOrientation(0); ImageView image = new ImageView(MainActivity.this); image.setImageResource(R.drawable.zyhang); TextView text = new TextView(MainActivity.this); text.setText("第" + (position +1 ) + "个一百天"); text.setTextSize(20); text.setTextColor(Color.RED); line.addView(image); line.addView(text); return line; } }; myList.setAdapter(adapter); } }
|
扩展BaseAdapter来实现Adapter对象,使用getCount()方法获取列表项的数量
AutoCompleteTextView(自动完成文本框)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<AutoCompleteTextView android:id="@+id/auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionHint="请选择您喜欢的领域:" android:dropDownHorizontalOffset="10dp" android:completionThreshold="1"/> <MultiAutoCompleteTextView android:id="@+id/mauto" android:layout_width="match_parent" android:layout_height="wrap_content" android:completionThreshold="1"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView;
public class MainActivity extends Activity { AutoCompleteTextView actv; MultiAutoCompleteTextView mauto; String[] books = new String[]{ "Python神经网络", "Python量化投资", "Python自动化运维", "Python数据科学" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, books); actv = (AutoCompleteTextView)findViewById(R.id.auto); actv.setAdapter(aa); mauto = (MultiAutoCompleteTextView)findViewById(R.id.mauto); mauto.setAdapter(aa); mauto.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer()); } }
|
使用AutoCompleteTextViewHR MultiAutoCompleteTextView,为他们那么绑定用一个Adapter提供提示文本
GridView(网络视图)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <GridView android:id="@+id/grid01" android:layout_width="match_parent" android:layout_height="wrap_content" android:horizontalSpacing="1pt" android:verticalSpacing="1pt" android:numColumns="4" android:gravity="center"/> <ImageView android:id="@+id/imageView" android:layout_width="240dp" android:layout_height="240dp" android:layout_gravity="center_horizontal"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.ImageView; import android.widget.SimpleAdapter;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
public class MainActivity extends Activity { GridView grid; ImageView imageView; int[] imageIds = new int[] { R.drawable.bomb5 , R.drawable.bomb6 , R.drawable.bomb7 , R.drawable.bomb8 , R.drawable.bomb9 , R.drawable.bomb10 , R.drawable.bomb11 , R.drawable.bomb12 , R.drawable.bomb13 , R.drawable.bomb14 , R.drawable.bomb15 , R.drawable.bomb16 }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < imageIds.length; i++) { Map<String, Object> listItem = new HashMap<String, Object>(); listItem.put("image", imageIds[i]); listItems.add(listItem); } imageView = (ImageView) findViewById(R.id.imageView); SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems , R.layout.cell, new String[] { "image" }, new int[] { R.id.image1 }); grid = (GridView) findViewById(R.id.grid01); grid.setAdapter(simpleAdapter); grid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { imageView.setImageResource(imageIds[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); grid.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { imageView.setImageResource(imageIds[position]); } }); } }
|
定义GridView参数,使用SimpleAdapter作为GridView的Adapter对16个组件进行List集合
ExpandableListView(可展开的列表组件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView;
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ExpandableListAdapter adapter = new BaseExpandableListAdapter() { int[] logos = new int[] { R.drawable.p, R.drawable.z, R.drawable.t }; private String[] armTypes = new String[] { "神族兵种", "虫族兵种", "人族兵种"}; private String[][] arms = new String[][] { { "狂战士", "龙骑士", "黑暗圣堂", "电兵" }, { "小狗", "刺蛇", "飞龙", "自爆飞机" }, { "机枪兵", "护士MM" , "幽灵" } }; @Override public Object getChild(int groupPosition, int childPosition) { return arms[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public int getChildrenCount(int groupPosition) { return arms[groupPosition].length; } private TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, 64); TextView textView = new TextView(MainActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36, 0, 0, 0); textView.setTextSize(10); return textView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition) .toString()); return textView; } @Override public Object getGroup(int groupPosition) { return armTypes[groupPosition]; } @Override public int getGroupCount() { return armTypes.length; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(MainActivity.this); ll.setOrientation(0); ImageView logo = new ImageView(MainActivity.this); logo.setImageResource(logos[groupPosition]); ll.addView(logo); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); ll.addView(textView); return ll; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean hasStableIds() { return true; } }; ExpandableListView expandListView = (ExpandableListView) findViewById(R.id.list); expandListView.setAdapter(adapter); } }
|
扩展BaseExpandaBleListAdapter来实现ExpandableListAdapter,通过getChlidView()返回一个普通TextView(),因为每个子列表项都是一个普通的文本框,每个组列表项都是一由图片和文字组成。
Spinner(列表选择框)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/books" android:prompt="@string/tip"/> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/tip"/> </LinearLayout> values <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="books"> <item>Axure RP</item> <item>Mockplus</item> <item>墨刀</item> </string-array> </resources>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.Spinner;
public class MainActivity extends Activity { Spinner spinner; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinner = (Spinner) findViewById(R.id.spinner); String[] arr = { "Oracle", "Redis", "MongoDB" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arr); spinner.setAdapter(adapter); } }
|
Gallery与Spinner:前者是一个垂直的列表选择框,后者是一个水平的列表选择框;前者供用户选择,厚泽允许用户通过拖动来查看上一个/下一个列表项
AdapterViewFlipper(图片轮播)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <?xml version="1.0" encoding="utf-8" ?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <AdapterViewFlipper android:id="@+id/flipper" android:layout_width="match_parent" android:layout_height="match_parent" android:flipInterval="5000" android:layout_alignParentTop="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:onClick="prev" android:text="上一个"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:onClick="next" android:text="下一个"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:onClick="auto" android:text="自动播放"/> </RelativeLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import static android.view.ViewGroup.LayoutParams; import android.widget.AdapterViewFlipper; import android.widget.BaseAdapter; import android.widget.ImageView;
public class MainActivity extends Activity { int[] imageIds = new int[] { R.drawable.shuangzi, R.drawable.shuangyu, R.drawable.chunv, R.drawable.tiancheng, R.drawable.tianxie, R.drawable.sheshou, R.drawable.juxie, R.drawable.shuiping, R.drawable.shizi, R.drawable.baiyang, R.drawable.jinniu, R.drawable.mojie }; private AdapterViewFlipper flipper; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); flipper = (AdapterViewFlipper) findViewById(R.id.flipper); BaseAdapter adapter = new BaseAdapter() { @Override public int getCount() { return imageIds.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(MainActivity.this); imageView.setImageResource(imageIds[position]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new ViewGroup.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); return imageView; } }; flipper.setAdapter(adapter); } public void prev(View source) { flipper.showPrevious(); flipper.stopFlipping(); } public void next(View source) { flipper.showNext(); flipper.stopFlipping(); } public void auto(View source) { flipper.startFlipping(); } }
|
使用showPrevious()、showNext()来控制组件显示上一个、下一个组件,调用了startFlipping()方法控制自动播放
StackView(堆叠视图)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <StackView android:id="@+id/mStackView" android:layout_width="match_parent" android:layout_height="wrap_content" android:loopViews="true" /> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一个" android:onClick="prev"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一个" android:onClick="next"/> </LinearLayout> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.SimpleAdapter; import android.widget.StackView;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
public class MainActivity extends Activity { StackView stackView; int[] imageIds = new int[] { R.drawable.bomb5 , R.drawable.bomb6 , R.drawable.bomb7 , R.drawable.bomb8 , R.drawable.bomb9 , R.drawable.bomb10 , R.drawable.bomb11 , R.drawable.bomb12 , R.drawable.bomb13 , R.drawable.bomb14 , R.drawable.bomb15 , R.drawable.bomb16 }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); stackView = (StackView) findViewById(R.id.mStackView); List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < imageIds.length; i++) { Map<String, Object> listItem = new HashMap<String, Object>(); listItem.put("image", imageIds[i]); listItems.add(listItem); } SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems , R.layout.cell, new String[] { "image" }, new int[] { R.id.image1 }); stackView.setAdapter(simpleAdapter); } public void prev(View view) { stackView.showPrevious(); } public void next(View view) { stackView.showNext(); } }
|
渐变褪去效果
创建SimpleAdapter并将SimpleAdapter设置为StackView的Adapter,则会显示一系列的组件。
ProgressBar类
ProgressBar(进度条)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Large"/> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Small"/> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="任务完成的进度"/> <ProgressBar android:id="@+id/bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" style="@android:style/Widget.ProgressBar.Horizontal"/> <ProgressBar android:id="@+id/bar2" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progressDrawable="@drawable/my_bar" style="@android:style/Widget.ProgressBar.Horizontal"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9
| <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:drawable="@drawable/no" /> <item android:id="@android:id/progress" android:drawable="@drawable/ok" /> </layer-list>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.widget.ProgressBar;
public class MainActivity extends Activity { private int[] data = new int[100]; int hasData = 0; int status = 0; ProgressBar bar , bar2; Handler mHandler = new Handler() { @Override
public void handleMessage(Message msg) { if (msg.what == 0x111) { bar.setProgress(status); bar2.setProgress(status); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bar = (ProgressBar) findViewById(R.id.bar); bar2 = (ProgressBar) findViewById(R.id.bar2); new Thread() { public void run() { while (status < 100) { status = doWork(); mHandler.sendEmptyMessage(0x111); } } }.start(); } public int doWork() { data[hasData++] = (int) (Math.random() * 100); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return hasData; } }
|
定义三个环形进度条和一个水平进度条, 通过修改进度完成进度来显示慢慢变满的进度条
SeekBar(拖动条)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="240dp" android:src="@drawable/lijiang"/> <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" android:progress="255" android:thumb="@drawable/zyhang"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import android.widget.SeekBar; import static android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity { ImageView image; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView) findViewById(R.id.image); SeekBar seekBar = (SeekBar) findViewById(R.id.seekbar); seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar arg0, int progress, boolean fromUser) { image.setImageAlpha(progress); } @Override public void onStartTrackingTouch(SeekBar bar) { } @Override public void onStopTrackingTouch(SeekBar bar) { } }); } }
|
ImageView用于显示图片,SeekBar用于动态改变图片的透明度,通过thumb来修改拖动条的外观,监听拖动条的滑动位置来修改透明度的数值
RatingBar(星级评分条)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="240dp" android:src="@drawable/lijiang"/> <RatingBar android:id="@+id/rating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:max="255" android:progress="255" android:stepSize="0.5"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; import android.widget.RatingBar; import static android.widget.RatingBar.OnRatingBarChangeListener;
public class MainActivity extends Activity { ImageView image; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); image = (ImageView) findViewById(R.id.image); RatingBar ratingBar = (RatingBar) findViewById(R.id.rating); ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar arg0, float rating, boolean fromUser) { image.setImageAlpha((int) (rating * 255 / 5)); } });
} }
|
在上一个程序中使用RayingBar,通过ProgressBar控制属性;绑定监听器监听星际评分条的星级改变
ViewAnimator类
ViewSwitcher(图形切换)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ViewSwitcher android:id="@+id/viewSwitcher" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/button_prev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:onClick="prev" android:text="<" /> <Button android:id="@+id/button_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:onClick="next" android:text=">" /> </RelativeLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| package org.crazyit.ui;
import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.TextView; import android.widget.ViewSwitcher;
import java.util.ArrayList;
public class MainActivity extends Activity { public static final int NUMBER_PER_SCREEN = 12; public static class DataItem { public String dataName; public Drawable drawable; } private ArrayList<DataItem> items = new ArrayList<DataItem>(); private int screenNo = -1; private int screenCount; ViewSwitcher switcher; LayoutInflater inflater; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); inflater = LayoutInflater.from(MainActivity.this); for (int i = 0; i < 40; i++) { String label = "" + i; Drawable drawable = getResources().getDrawable( R.drawable.zyhang); DataItem item = new DataItem(); item.dataName = label; item.drawable = drawable; items.add(item); } screenCount = items.size() % NUMBER_PER_SCREEN == 0 ? items.size()/ NUMBER_PER_SCREEN : items.size() / NUMBER_PER_SCREEN + 1; switcher = (ViewSwitcher) findViewById(R.id.viewSwitcher); switcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { return inflater.inflate(R.layout.slidelistview, null); } }); next(null); } public void next(View v) { if (screenNo < screenCount - 1) { screenNo++; switcher.setInAnimation(this, R.anim.slide_in_right); switcher.setOutAnimation(this, R.anim.slide_out_left); ((GridView) switcher.getNextView()).setAdapter(adapter); switcher.showNext(); } } public void prev(View v) { if (screenNo > 0) { screenNo--; switcher.setInAnimation(this, android.R.anim.slide_in_left); switcher.setOutAnimation(this, android.R.anim.slide_out_right); ((GridView) switcher.getNextView()).setAdapter(adapter); switcher.showPrevious(); } } private BaseAdapter adapter = new BaseAdapter() { @Override public int getCount() { if (screenNo == screenCount - 1 && items.size() % NUMBER_PER_SCREEN != 0) { return items.size() % NUMBER_PER_SCREEN; } return NUMBER_PER_SCREEN; } @Override public DataItem getItem(int position) { return items.get(screenNo * NUMBER_PER_SCREEN + position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position , View convertView, ViewGroup parent) { View view = convertView; if (convertView == null) { view = inflater.inflate(R.layout.labelicon, null); } ImageView imageView = (ImageView) view.findViewById(R.id.imageview); imageView.setImageDrawable(getItem(position).drawable); TextView textView = (TextView) view.findViewById(R.id.textview); textView.setText(getItem(position).dataName); return view; } }; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" /> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime" /> </set> left <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="@android:integer/config_mediumAnimTime" /> </set>
|
使用ViewSwitcher组合多个GridView,使用扩展BaseAdapter方式为GridView提供Adapter,使用screenNo保存当前正在显示第几屏程序列表,使用BaseAdapter根据screenNo动态计算Adapter总共包含多少个列表项。
ImageSwitcher(图像切换器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <GridView android:id="@+id/grid01" android:layout_width="match_parent" android:layout_height="wrap_content" android:horizontalSpacing="2dp" android:verticalSpacing="2dp" android:numColumns="4" android:gravity="center"/> <ImageSwitcher android:id="@+id/switcher" android:layout_width="300dp" android:layout_height="300dp" android:layout_gravity="center_horizontal" android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| package org.crazyit.ui;
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.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ViewSwitcher.ViewFactory; import android.widget.GridView; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.SimpleAdapter;
public class MainActivity extends Activity { int[] imageIds = new int[] { R.drawable.bomb5 , R.drawable.bomb6 , R.drawable.bomb7 , R.drawable.bomb8 , R.drawable.bomb9 , R.drawable.bomb10 , R.drawable.bomb11 , R.drawable.bomb12 , R.drawable.bomb13 , R.drawable.bomb14 , R.drawable.bomb15 , R.drawable.bomb16 }; ImageSwitcher switcher; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < imageIds.length; i++) { Map<String, Object> listItem = new HashMap<String, Object>(); listItem.put("image", imageIds[i]); listItems.add(listItem); } switcher = (ImageSwitcher) findViewById(R.id.switcher); switcher.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return imageView; } }); SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems , R.layout.cell, new String[]{"image"}, new int[] { R.id.image1 }); GridView grid = (GridView) findViewById(R.id.grid01); grid.setAdapter(simpleAdapter); grid.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { switcher.setImageResource(imageIds[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); grid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switcher.setImageResource(imageIds[position]); } }); } }
|
通过inAnimation、outAnimation指定图片切换的动画效果为ImageSwitcher设置ViewFactory,通过绑定事件监听器监听单元格进行动画的切换效果
TextSwitcher(文本切换器)
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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" > <TextSwitcher android:id="@+id/textSwitcher" android:layout_width="match_parent" android:layout_height="wrap_content" android:inAnimation="@android:anim/slide_in_left" android:outAnimation="@android:anim/slide_out_right" android:onClick="next"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| package org.crazyit.ui;
import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextSwitcher; import android.widget.TextView; import android.widget.ViewSwitcher;
public class MainActivity extends Activity { TextSwitcher textSwitcher; String[] strs = new String[] { "ORM", "CORBA", "Web Service", "RPC" }; int curStr; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher); textSwitcher.setFactory(new ViewSwitcher.ViewFactory() { public View makeView() { TextView tv = new TextView(MainActivity.this); tv.setTextSize(40); tv.setTextColor(Color.MAGENTA); return tv; } }); next(null); } public void next(View source) { textSwitcher.setText(strs[curStr++ % strs.length]); } }
|
定义TextSwitcher并制定了文本切换的动画效果,再为TextSwitcher设置ViewFactory则可以正常运行
ViewFlipper(切换控件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ViewFlipper android:id="@+id/details" android:layout_width="match_parent" android:layout_height="match_parent" android:flipInterval="1000"> <ImageView android:src="@drawable/java" android:layout_width="match_parent" android:layout_height="wrap_content"> </ImageView> <ImageView android:src="@drawable/android" android:layout_width="match_parent" android:layout_height="wrap_content"> </ImageView> <ImageView android:src="@drawable/javaee" android:layout_width="match_parent" android:layout_height="wrap_content"> </ImageView> </ViewFlipper> <Button android:text="<" android:onClick="prev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerInParent="true" android:onClick="auto" android:text="自动播放"/> <Button android:text=">" android:onClick="next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true"/> </RelativeLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ViewFlipper;
public class MainActivity extends Activity { private ViewFlipper viewFlipper; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); viewFlipper = (ViewFlipper) findViewById(R.id.details); } public void prev(View source) { viewFlipper.setInAnimation(this , R.anim.slide_in_right); viewFlipper.setOutAnimation(this , R.anim.slide_out_left); viewFlipper.showPrevious(); viewFlipper.stopFlipping(); } public void next(View source) { viewFlipper.setInAnimation(this , android.R.anim.slide_in_left); viewFlipper.setOutAnimation(this , android.R.anim.slide_out_right); viewFlipper.showNext(); viewFlipper.stopFlipping(); } public void auto(View source) { viewFlipper.setInAnimation(this , android.R.anim.slide_in_left); viewFlipper.setOutAnimation(this , android.R.anim.slide_out_right); viewFlipper.startFlipping(); } }
|
在ViewFlipper定义了三个ImageView,意味着ViewFlipper包含三个子组件,在Activity中调用ViewFlipper得showPrevious()、showNext()等方法来显示上一个、下一个组件。
各种杂项控件
Toast(提示信息框)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package org.crazyit.ui;
import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Gravity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast;
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button simple = (Button) findViewById(R.id.simple); simple.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { Toast toast = Toast.makeText(MainActivity.this , "文字提示" , Toast.LENGTH_SHORT); toast.show(); } }); Button bn = (Button) findViewById(R.id.bn); bn.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { Toast toast = new Toast(MainActivity.this); toast.setGravity(Gravity.CENTER, 0, 0); ImageView image = new ImageView(MainActivity.this); image.setImageResource(R.drawable.a); LinearLayout ll = new LinearLayout(MainActivity.this); ll.addView(image); TextView textView = new TextView(MainActivity.this); textView.setText("图片提示"); textView.setTextSize(24); textView.setTextColor(Color.MAGENTA); ll.addView(textView); toast.setView(ll); toast.setDuration(Toast.LENGTH_LONG); toast.show(); } }); } }
|
一个按钮用于激发文字提示,一个按钮用于激发图片提示,需要调用Toast对象得setView()方法来改变该Toast对象得内容View
CalendarView(日历视图)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选择您的生日:"/>
<CalendarView android:layout_width="match_parent" android:layout_height="match_parent" android:firstDayOfWeek="3" android:shownWeekCount="4" android:selectedWeekBackgroundColor="#aff" android:focusedMonthDateColor="#f00" android:weekSeparatorLineColor="#ff0" android:unfocusedMonthDateColor="#f9f" android:id="@+id/calendarView" /> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CalendarView; import android.widget.CalendarView.OnDateChangeListener; import android.widget.Toast;
public class MainActivity extends Activity { CalendarView cv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); cv = (CalendarView)findViewById(R.id.calendarView); cv.setOnDateChangeListener(new OnDateChangeListener() { @Override public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { Toast.makeText(MainActivity.this, "你生日是" + year + "年" + month + "月" + dayOfMonth + "日", Toast.LENGTH_SHORT).show(); } }); } }
|
添加DatePicker、TimePicker来选择日期、时间,使用EditText来显示选择得日期、时间。绑定监听器,当选择日期、时间时、监听器负责使用EditText显示所选得时间、日期
DatePicker(日期选择器)&TimePicker(时间选择器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="选择购买本书的具体时间"/> <DatePicker android:id="@+id/datePicker" android:layout_width="wrap_content" android:layout_height="200dp" android:layout_gravity="center_horizontal" android:startYear="2000" android:endYear="2016" android:calendarViewShown="true" android:spinnersShown="true"/> <TimePicker android:id="@+id/timePicker" android:layout_width="wrap_content" android:layout_height="100dp" android:layout_gravity="center_horizontal"/> <EditText android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.EditText; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener;
import java.util.Calendar;
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.datePicker); TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker); Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); hour = c.get(Calendar.HOUR); minute = c.get(Calendar.MINUTE); datePicker.init(year, month, day, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker arg0, int year , int month, int day) { MainActivity.this.year = year; MainActivity.this.month = month; MainActivity.this.day = day; showDate(year, month, day, hour, minute); } }); timePicker.setEnabled(true); timePicker.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view , int hourOfDay, int minute) { MainActivity.this.hour = hourOfDay; MainActivity.this.minute = minute; showDate(year, month, day, hour, minute); } }); } private void showDate(int year, int month , int day, int hour, int minute) { EditText show = (EditText) findViewById(R.id.show); show.setText("接触安卓的时间为:" + year + "年" + (month + 1) + "月" + day + "日 " + hour + "时" + minute + "分"); } }
|
tianjia DatePicker、TimePicker用于选择日期、时间,还包含EditText用于显示日期、时间,绑定事件监听器,当监听器触发时,使用EditText显示选择得日期、时间
NumberPicker(数值选择器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="选择低价:" android:layout_width="120dp" android:layout_height="wrap_content" /> <NumberPicker android:id="@+id/np1" android:layout_width="match_parent" android:layout_height="80dp" android:focusable="true" android:focusableInTouchMode="true" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="选择高价:" android:layout_width="120dp" android:layout_height="wrap_content" /> <NumberPicker android:id="@+id/np2" android:layout_width="match_parent" android:layout_height="80dp" android:focusable="true" android:focusableInTouchMode="true" /> </TableRow> </TableLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.NumberPicker; import android.widget.NumberPicker.OnValueChangeListener; import android.widget.Toast;
public class MainActivity extends Activity { NumberPicker np1, np2; int minPrice = 25, maxPrice = 75; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); np1 = (NumberPicker) findViewById(R.id.np1); np1.setMinValue(10); np1.setMaxValue(50); np1.setValue(minPrice); np1.setOnValueChangedListener(new OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { minPrice = newVal; showSelectedPrice(); } }); np2 = (NumberPicker) findViewById(R.id.np2); np2.setMinValue(60); np2.setMaxValue(100); np2.setValue(maxPrice); np2.setOnValueChangedListener(new OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { maxPrice = newVal; showSelectedPrice(); } }); } private void showSelectedPrice() { Toast.makeText(this, "您选择最低价格为:" + minPrice + ",最高价格为:" + maxPrice, Toast.LENGTH_SHORT) .show(); } }
|
定义了两个NumberPicker并通过setMinValue()、setMaxValue()、setValue()为这两个NumberPicker设置最小值、最大值、当前值,并为最大值和最小值绑定监听器
SearchView(搜索框)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?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="vertical"> <SearchView android:id="@+id/sv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> </LinearLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SearchView; import android.widget.SearchView.OnQueryTextListener; import android.widget.Toast;
public class MainActivity extends Activity { private SearchView sv; private ListView lv; private final String[] mStrings = { "aaaaa", "bbbbbb", "cccccc" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView) findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings)); lv.setTextFilterEnabled(true); sv = (SearchView) findViewById(R.id.sv); sv.setIconifiedByDefault(false); sv.setSubmitButtonEnabled(true); sv.setQueryHint("查找"); sv.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextChange(String newText) { if (TextUtils.isEmpty(newText)) { lv.clearTextFilter(); } else { lv.setFilterText(newText); } return true; } @Override public boolean onQueryTextSubmit(String query) { Toast.makeText(MainActivity.this, "您的选择是:" + query , Toast.LENGTH_SHORT).show(); return false; } }); } }
|
定义SearchView组件,并为该组件定义ListView组件,用于显示自动完成列表。设置监听器,并未组件恰东了搜索按钮,重写onQueryTextChange()、onQueryTextSubmit()方法用于SearchView时间提供响应
TabHost(选项卡)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="钢铁侠 - 2019/05/01" android:textSize="11pt" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="绿巨人 - 2019/05/02" android:textSize="11pt" /> </LinearLayout> <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="美国队长 - 2019/04/30" android:textSize="11pt" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="黑寡妇 - 2019/04/29" android:textSize="11pt" /> </LinearLayout> <LinearLayout android:id="@+id/tab03" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:textSize="11pt"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="惊奇队长 - 2019/05/03" android:textSize="11pt" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="雷神 - 2019/05/04" android:textSize="11pt" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package org.crazyit.ui;
import android.app.TabActivity; import android.os.Bundle; import android.widget.TabHost;
public class MainActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TabHost tabHost = getTabHost(); TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1") .setIndicator("已接电话") .setContent(R.id.tab01); tabHost.addTab(tab1); TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2") .setIndicator("呼出电话", getResources() .getDrawable(R.drawable.ic_launcher)) .setContent(R.id.tab02); tabHost.addTab(tab2); TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3") .setIndicator("未接电话") .setContent(R.id.tab03); tabHost.addTab(tab3); } }
|
TabHost容器组合了TabWidget和FrameLayout。其中TabWidget用于定义选项卡的标题条、FrameLayout用于“折叠组合多个选项页面。使用setConnect()方法设置标签页内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Python" android:textSize="30dp" /> </LinearLayout> </HorizontalScrollView> </ScrollView>
|
实现垂直水平滚动
Notification(手机状态栏)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package org.crazyit.ui;
import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View;
public class MainActivity extends Activity { static final int NOTIFICATION_ID = 0x123; NotificationManager nm; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } public void send(View source) { Intent intent = new Intent(MainActivity.this , OtherActivity.class); PendingIntent pi = PendingIntent.getActivity( MainActivity.this, 0, intent, 0); Notification notify = new Notification.Builder(this) .setAutoCancel(true) .setTicker("有新消息") .setSmallIcon(R.drawable.notify) .setContentTitle("一条新通知") .setContentText("恭喜你,您获得了人民奖学金一等奖") .setSound(Uri.parse("android.resource://org.crazyit.ui/" + R.raw.msg)) .setWhen(System.currentTimeMillis()) .setContentIntent(pi) .build(); nm.notify(NOTIFICATION_ID, notify); } public void del(View v) { nm.cancel(NOTIFICATION_ID); } }
|
设置Notification的图表、标题、发送时间,声音提示、震动提示、闪光灯等
对话框
AlertDialog对话框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/loginForm" android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="用户名:" android:textSize="10pt"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写登录账号" android:selectAllOnFocus="true"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="密码:" android:textSize="10pt"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写密码" android:password="true"/> </TableRow> <TableRow> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="电话号码:" android:textSize="10pt"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请填写您的电话号码" android:selectAllOnFocus="true" android:phoneNumber="true"/> </TableRow> </TableLayout>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| package org.crazyit.ui;
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.TableLayout; import android.widget.TextView;
public class MainActivity extends Activity { TextView show; String[] items = new String[] { "感知器", "线性单元和梯度下降", "神经网络和反向传播算法", "卷积神经网络" };
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); show = (TextView) findViewById(R.id.show); }
public void simple(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("简单对话框") .setIcon(R.drawable.tools) .setMessage("对话框的测试内容\n第二行内容"); setPositiveButton(builder); setNegativeButton(builder) .create() .show(); }
public void simpleList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("简单列表对话框") .setIcon(R.drawable.tools) .setItems(items, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你选中了《" + items[which] + "》"); } }); setPositiveButton(builder); setNegativeButton(builder) .create() .show(); }
public void singleChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("单选列表项对话框") .setIcon(R.drawable.tools) .setSingleChoiceItems(items, 1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你选中了《" + items[which] + "》"); } }); setPositiveButton(builder); setNegativeButton(builder) .create() .show(); }
public void multiChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("多选列表项对话框") .setIcon(R.drawable.tools) .setMultiChoiceItems(items , new boolean[]{false , true ,false ,true}, null); setPositiveButton(builder); setNegativeButton(builder) .create() .show(); }
public void customList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle("自定义列表项对话框") .setIcon(R.drawable.tools) .setAdapter(new ArrayAdapter<String>(this , R.layout.array_item , items), null); setPositiveButton(builder); setNegativeButton(builder) .create() .show(); }
public void customView(View source) { TableLayout loginForm = (TableLayout)getLayoutInflater() .inflate( R.layout.login, null); new AlertDialog.Builder(this) .setIcon(R.drawable.tools) .setTitle("自定义View对话框") .setView(loginForm) .setPositiveButton("登录", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .create() .show(); }
private AlertDialog.Builder setPositiveButton( AlertDialog.Builder builder) { return builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("单击了【确定】按钮!"); } }); } private AlertDialog.Builder setNegativeButton( AlertDialog.Builder builder) { return builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("单击了【取消】按钮!"); } }); } }
|
设置图标、标题等属性,添加变失性按钮
调用setItems()方法为对话框设置多个列表项
调用setSingleChoiceItems()方法创建单选列表表项的对话框,闯入ListAdapter参数提供多个列表项组件
调用setMultiItems()方法传入数组参数和数据库查询结果集参数
调用setAdapter()方法设置对话框的内容,传入Adapter参数负责提供多个列表项组件
使用setView()方法作为对话框的内容,调用setView()设置自定义视图
对话框风格的窗口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button bn = (Button) findViewById(R.id.bn); bn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { finish(); } }); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.PopupWindow;
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View root = this.getLayoutInflater().inflate(R.layout.popup, null); final PopupWindow popup = new PopupWindow(root, 560, 720); Button button = (Button) findViewById(R.id.bn); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { popup.showAtLocation(findViewById(R.id.bn), Gravity.CENTER, 20,20); } }); root.findViewById(R.id.close).setOnClickListener( new View.OnClickListener() { public void onClick(View v) { popup.dismiss(); } }); } }
|
popup.dismiss负责销毁。隐藏对象关闭时会自动关闭窗口
DatePickerDialog & TimePickerDialog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| package org.crazyit.ui;
import android.app.Activity; import android.app.DatePickerDialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker;
import java.util.Calendar;
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button dateBn = (Button)findViewById(R.id.dateBn); Button timeBn = (Button)findViewById(R.id.timeBn); dateBn.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { Calendar c = Calendar.getInstance(); new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) { EditText show = (EditText) findViewById(R.id.show); show.setText("您选择了:" + year + "年" + (month + 1) + "月" + dayOfMonth + "日"); } } , c.get(Calendar.YEAR) , c.get(Calendar.MONTH) , c.get(Calendar.DAY_OF_MONTH)).show(); } }); timeBn.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { Calendar c = Calendar.getInstance(); new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker tp, int hourOfDay, int minute) { EditText show = (EditText) findViewById(R.id.show); show.setText("您选择了:" + hourOfDay + "时" + minute + "分"); } } , c.get(Calendar.HOUR_OF_DAY) , c.get(Calendar.MINUTE) , true).show(); } }); } }
|
通过new关键字创建DatePickerDialog实例,调用show()方法将日期选择对话框、时间选择对话框显示出来。绑定监听器,通过监听器获取用户设置的事件。
ProcessDialog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| package org.crazyit.ui;
import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View;
public class MainActivity extends Activity { final static int MAX_PROGRESS = 100; private int[] data = new int[50]; int progressStatus = 0; int hasData = 0; ProgressDialog pd1,pd2; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0x123) { pd2.setProgress(progressStatus); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void showSpinner(View source) { ProgressDialog.show(this, "任务执行中" , "任务执行中,请等待", false, true); } public void showIndeterminate(View source) { pd1 = new ProgressDialog(MainActivity.this); pd1.setTitle("任务正在执行中"); pd1.setMessage("任务正在执行中,敬请等待..."); pd1.setCancelable(true); pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd1.setIndeterminate(true); pd1.show(); } public void showProgress(View source) { progressStatus = 0; hasData = 0; pd2 = new ProgressDialog(MainActivity.this); pd2.setMax(MAX_PROGRESS); pd2.setTitle("任务完成百分比"); pd2.setMessage("耗时任务的完成百分比"); pd2.setCancelable(false); pd2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd2.setIndeterminate(false); pd2.show(); new Thread() { public void run() { while (progressStatus < MAX_PROGRESS) { progressStatus = MAX_PROGRESS * doWork() / data.length; handler.sendEmptyMessage(0x123); } if (progressStatus >= MAX_PROGRESS) { pd2.dismiss(); } } }.start(); } public int doWork() { data[hasData++] = (int) (Math.random() * 100); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } return hasData; } }
|
第一个按钮事件处理调用ProcessDialog()静态show()方法创建并显示对话框,第二个按钮事件处理先创建ProcessDialog对象,再调用show()方法将它显示出来,第三个按钮事件处理先创建ProcessDialog对象,设置对话框相关属性,再调用show()方法将它显示出来
菜单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package org.crazyit.ui;
import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.SubMenu; import android.widget.EditText; import android.widget.Toast;
public class MainActivity extends Activity { final int FONT_10 = 0x111; final int FONT_12 = 0x112; final int FONT_14 = 0x113; final int FONT_16 = 0x114; final int FONT_18 = 0x115; final int PLAIN_ITEM = 0x11b; final int FONT_RED = 0x116; final int FONT_BLUE = 0x117; final int FONT_GREEN = 0x118; private EditText edit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edit = (EditText) findViewById(R.id.txt); } @Override public boolean onCreateOptionsMenu(Menu menu) { SubMenu fontMenu = menu.addSubMenu("字体大小"); fontMenu.setIcon(R.drawable.font); fontMenu.setHeaderIcon(R.drawable.font); fontMenu.setHeaderTitle("选择字体大小"); fontMenu.add(0, FONT_10, 0, "10号字体"); fontMenu.add(0, FONT_12, 0, "12号字体"); fontMenu.add(0, FONT_14, 0, "14号字体"); fontMenu.add(0, FONT_16, 0, "16号字体"); fontMenu.add(0, FONT_18, 0, "18号字体"); menu.add(0, PLAIN_ITEM, 0, "普通菜单项"); SubMenu colorMenu = menu.addSubMenu("字体颜色"); colorMenu.setIcon(R.drawable.color); colorMenu.setHeaderIcon(R.drawable.color); colorMenu.setHeaderTitle("选择文字颜色"); colorMenu.add(0, FONT_RED, 0, "红色"); colorMenu.add(0, FONT_GREEN, 0, "绿色"); colorMenu.add(0, FONT_BLUE, 0, "蓝色"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem mi) { switch (mi.getItemId()) { case FONT_10: edit.setTextSize(10 * 2); break; case FONT_12: edit.setTextSize(12 * 2); break; case FONT_14: edit.setTextSize(14 * 2); break; case FONT_16: edit.setTextSize(16 * 2); break; case FONT_18: edit.setTextSize(18 * 2); break; case FONT_RED: edit.setTextColor(Color.RED); break; case FONT_GREEN: edit.setTextColor(Color.GREEN); break; case FONT_BLUE: edit.setTextColor(Color.BLUE); break; case PLAIN_ITEM: Toast toast = Toast.makeText(MainActivity.this , "您单击了普通菜单项" , Toast.LENGTH_SHORT); toast.show(); break; } return true; } }
|
创建多选菜单想和单选菜单项,添加三个菜单,两个有子菜单,设置了图标、标题,调用onOptionsItemSelected()方法,可以为菜单项的单击事件提供相应,使用监听器监听菜单事件。
上下文菜单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| package org.crazyit.ui;
import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.TextView;
public class MainActivity extends Activity { final int MENU1 = 0x111; final int MENU2 = 0x112; final int MENU3 = 0x113; private TextView txt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt = (TextView) findViewById(R.id.txt); registerForContextMenu(txt); } @Override public void onCreateContextMenu(ContextMenu menu, View source, ContextMenu.ContextMenuInfo menuInfo) { menu.add(0, MENU1, 0, "红色"); menu.add(0, MENU2, 0, "绿色"); menu.add(0, MENU3, 0, "蓝色"); menu.setGroupCheckable(0, true, true); menu.setHeaderIcon(R.drawable.tools); menu.setHeaderTitle("选择背景色"); } @Override public boolean onContextItemSelected(MenuItem mi) { switch (mi.getItemId()) { case MENU1: mi.setChecked(true); txt.setBackgroundColor(Color.RED); break; case MENU2: mi.setChecked(true); txt.setBackgroundColor(Color.GREEN); break; case MENU3: mi.setChecked(true); txt.setBackgroundColor(Color.BLUE); break; } return true; } }
|
重写onCreateContextMenu()方法创建上下文菜单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package org.crazyit.ui;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.PopupMenu; import android.widget.Toast;
public class MainActivity extends Activity { PopupMenu popup = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onPopupButtonClick(View button) { popup = new PopupMenu(this, button); getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu()); popup.setOnMenuItemClickListener( new PopupMenu.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.exit: popup.dismiss(); break; default: Toast.makeText(MainActivity.this, "您单击了【" + item.getTitle() + "】菜单项" , Toast.LENGTH_SHORT).show(); } return true; } }); popup.show(); } }
|
调用MenuInflater的inflate()方法将菜单资源填充到PopopMenu,调用show()方法显示弹出式菜单。
ActionBar(活动条)
ActionBar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package org.crazyit.ui;
import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View;
public class MainActivity extends Activity { ActionBar actionBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); actionBar = getActionBar(); } public void showActionBar(View source) { actionBar.show(); } public void hideActionBar(View source) { actionBar.hide(); } }
|
调用getActionBar()方法获取Activity关联的ActionBar
ActionBar显示选项菜单项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/font_size" android:showAsAction="always|withText" android:icon="@drawable/font"> <menu> <group android:checkableBehavior="single"> <item android:id="@+id/font_10" android:title="@string/font_10"/> <item android:id="@+id/font_12" android:title="@string/font_12"/> <item android:id="@+id/font_14" android:title="@string/font_14"/> <item android:id="@+id/font_16" android:title="@string/font_16"/> <item android:id="@+id/font_18" android:title="@string/font_18"/> </group> </menu> </item> <item android:id="@+id/plain_item" android:showAsAction="always|withText" android:title="@string/plain_item"> </item> <item android:title="@string/font_color" android:showAsAction="always" android:icon="@drawable/color"> <menu> <group> <item android:id="@+id/red_font" android:title="@string/red_title"/> <item android:id="@+id/green_font" android:title="@string/green_title"/> <item android:id="@+id/blue_font" android:title="@string/blue_title"/> </group> </menu> </item> </menu>
|
为选项菜单项增加showAsAction属性控制将惨淡想显示在ActionBar上
程序图标导航
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package org.crazyit.ui;
import android.app.ActionBar; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast;
public class MainActivity extends Activity { private TextView txt; ActionBar actionBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txt = (TextView) findViewById(R.id.txt); actionBar = getActionBar(); actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflator = new MenuInflater(this); inflator.inflate(R.menu.menu_main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem mi) { if(mi.isCheckable()) { mi.setChecked(true); } switch (mi.getItemId()) { case android.R.id.home: Intent intent = new Intent(this, FirstActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); break; case R.id.font_10: txt.setTextSize(10 * 2); break; case R.id.font_12: txt.setTextSize(12 * 2); break; case R.id.font_14: txt.setTextSize(14 * 2); break; case R.id.font_16: txt.setTextSize(16 * 2); break; case R.id.font_18: txt.setTextSize(18 * 2); break; case R.id.red_font: txt.setTextColor(Color.RED); mi.setChecked(true); break; case R.id.green_font: txt.setTextColor(Color.GREEN); mi.setChecked(true); break; case R.id.blue_font: txt.setTextColor(Color.BLUE); mi.setChecked(true); break; case R.id.plain_item: Toast toast = Toast.makeText(MainActivity.this, "您单击了普通菜单项", Toast.LENGTH_SHORT); toast.show(); break; } return true; } }
|
绑定事件监听器,点击ID为Action Item时,程序使用Intent返回FirstActivity
Action View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?xml version="1.0" encoding="utf-8" ?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/search" android:orderInCategory="100" android:showAsAction="always" android:actionViewClass="android.widget.SearchView"/> <item android:id="@+id/progress" android:orderInCategory="100" android:showAsAction="always" android:actionLayout="@layout/clock"/> </menu> layout <?xml version="1.0" encoding="utf-8" ?> <AnalogClock xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" />
|
定义Aciton View,制定Action View的实现类为Search View,制定Action View对应的界面布局资源,并定义AnalogClock模拟时钟
ActionBar显示Tab导航
1 2 3 4 5 6 7
| <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" />
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| package org.crazyit.ui;
import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem;
public class MainActivity extends Activity implements ActionBar.TabListener { private static final String SELECTED_ITEM = "selected_item"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.addTab(actionBar.newTab().setText("第一页") .setTabListener(this)); actionBar.addTab(actionBar.newTab().setText("第二页") .setTabListener(this)); actionBar.addTab(actionBar.newTab().setText("第三页") .setTabListener(this)); } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { if (savedInstanceState.containsKey(SELECTED_ITEM)) { getActionBar().setSelectedNavigationItem( savedInstanceState.getInt(SELECTED_ITEM)); } } @Override public void onSaveInstanceState(Bundle outState) { outState.putInt(SELECTED_ITEM, getActionBar().getSelectedNavigationIndex()); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { Fragment fragment = new DummyFragment(); Bundle args = new Bundle(); args.putInt(DummyFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); fragment.setArguments(args); FragmentTransaction ft = getFragmentManager() .beginTransaction(); ft.replace(R.id.container, fragment); ft.commit(); } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } }
|
DummyFragment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package org.crazyit.ui;
import android.app.Fragment; import android.os.Bundle;
import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView;
public class DummyFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setGravity(Gravity.CENTER_HORIZONTAL); Bundle args = getArguments(); textView.setText(args.getInt(ARG_SECTION_NUMBER) + ""); textView.setTextSize(30); return textView; } }
|
定义LinearLayout作为容器,动态承装Fragment,并为三个ActionBar添加了三个Tab标签,每个标签设置了事件监听器,通过激发onTabSelected()方法,替换新的Fragment
ActionBar显示下拉式菜单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| package org.crazyit.ui;
import android.app.ActionBar; import android.app.Activity; import android.app.Fragment; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter;
public class MainActivity extends Activity implements ActionBar.OnNavigationListener { private static final String SELECTED_ITEM = "selected_item"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final ActionBar actionBar = getActionBar(); actionBar.setDisplayShowTitleEnabled(true); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); actionBar.setListNavigationCallbacks( new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, new String[] {"第一页","第二页","第三页" }), this); } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { if (savedInstanceState.containsKey(SELECTED_ITEM)) { getActionBar().setSelectedNavigationItem( savedInstanceState.getInt(SELECTED_ITEM)); } } @Override public void onSaveInstanceState(Bundle outState) { outState.putInt(SELECTED_ITEM, getActionBar().getSelectedNavigationIndex()); } @Override public boolean onNavigationItemSelected(int position, long id) { Fragment fragment = new DummyFragment(); Bundle args = new Bundle(); args.putInt(DummyFragment.ARG_SECTION_NUMBER, position + 1); fragment.setArguments(args); FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.replace(R.id.container, fragment); ft.commit(); return true; } }
|
调用setNavigationMode()启动下拉列表导航支持,为ActionBar传入ArrAyAdapter和监听器,选中指定的导航项时,激发监听器onNavigationItemSelected()
本文使用 CC BY-NC-SA 3.0 中国大陆 协议许可
具体请参见 知识共享协议
本文链接:https://zyhang8.github.io/2019/10/23/android-exp2/