Android开发03—Android常用基本控件(下)
1. 单选按钮和复选按钮
CheckBox和RadioButton控件都只有选中和未选中两种状态,不同的是RadioButton是单选按钮,需要便知道RadioGroup中,同一时刻一个RadioGroup中只能有一个按钮处于选中状态。
使用举例:
ImageView控件负责显示图片,其图片的来源既可以是资源文件的id,也可以是Drawable对象或BitMap对象,还可以是ContentProvider的Uri。
实例:ImageView的用法
string.xml
Activity部分代码:
3. 时钟控件
AnalogClock和DigitalClock都负责显示时钟,所不同的是AnalogClock控件显示模拟时钟,且只显示时针和分针,而digital显示数字时钟,可精确到秒
直接在main.xml中定义时钟控件即可。
4. 日期与时间选择控件
DatePicker继承自FrameLayout类,主要功能是向用户提供包含年,月,日的日期数据,并允许用户进行选择。如果要捕获用户修改日期选择控件中数据的时间,需要为DatePicker添加onDateChangedListener监听器。
TimePicker同样继承自FrameLayout类。时间选择控件向用户显示一天中的时间,并允许用户进行选择,如果要捕获用户修改时间数据的时间,便需要为TimePicker添加OnTimeChangedListener监听器。
案例:
main.xml
Activity:
5. 动画播放技术
动画播放技术主要有两种:帧动画和补间动画。
1) 帧动画
帧动画主要用到的类是AnimationDrawable,每个帧动画都是一个AnimationDrawable对象
定义帧动画可以在代码中直接进行,也可以通过XML文件进行定义,定义帧动画的XML文件将存放在res/anim目录下。XML文件中指定了图片图片帧出现的顺序及每个帧的连续时间。
anim.xml
main.xml
Activity:
补间动画作用于View对象,主要包括View对象的位置,尺寸,旋转角度和透明度的变换。
补间动画可以通过XML声明也可以在代码中动态定义。推荐使用XML因为XML文件可读性及可用性高,方便替换。
1. 单选按钮和复选按钮
CheckBox和RadioButton控件都只有选中和未选中两种状态,不同的是RadioButton是单选按钮,需要便知道RadioGroup中,同一时刻一个RadioGroup中只能有一个按钮处于选中状态。
使用举例:
public class SampleDemo extends Activity { public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstatceState); setContentView(R.layout.main); CheckBox cb = (CheckBox) findViewById(R.id.CheckBox1); cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){ public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){ setBulbState(isChecked); } }); RadioButton rb = (RadioButton) findViewById(R.id.off); rb.setOnCheckedChangeListener(new OnCheckedChangeListener(){ public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){ setBulbState(isChecked); } }); } public void setBulbState(boolean state){ ImageView iv = (ImageView) findViewById(R.id.ImageView01); iv.setImageResource((state)?R.drawable.bulb_on:R.drawable.bulb_off); CheckBox cb = (CheckBox) this.findViewById(R.id.CheckBox1); cb.setText((state)?R.string.off:R.string.on); cb.setChecked(state); RadioButton rb = (RadioButton) this.findViewById(R.id.off); rb.setChecked(state); rb = (RadioButton) findViewById(R.id.on); rb.setChecked(state); } }2. 图片控件
ImageView控件负责显示图片,其图片的来源既可以是资源文件的id,也可以是Drawable对象或BitMap对象,还可以是ContentProvider的Uri。
实例:ImageView的用法
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">AbsoluteExample</string> <string name="next">下一张</string> <string name="previous">上一张</string> <string name="alpha_plus">透明度增加</string> <string name="alpha_minus">透明度减少</string> </resources>main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv" android:layout_gravity="center_horizontal" android:src="@drawable/pic1" ></ImageView> <LinearLayout android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:orientation="horizontal"> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/previous" android:text="@string/previous" android:gravity="center_horizontal" android:layout_gravity="center_horizontal"></Button> <Button android:layout_height="wrap_content" android:id="@+id/alpha_plus" android:layout_width="wrap_content" android:text="@string/alpha_plus" android:gravity="center_horizontal" android:layout_gravity="center_horizontal"></Button> <Button android:layout_height="wrap_content" android:id="@+id/alpha_minus" android:layout_width="wrap_content" android:text="@string/alpha_minus" android:gravity="center_horizontal" android:layout_gravity="center_horizontal"></Button> <Button android:layout_height="wrap_content" android:id="@+id/next" android:layout_width="wrap_content" android:text="@string/next" android:gravity="center_horizontal" android:layout_gravity="center_horizontal"></Button> </LinearLayout> </LinearLayout>
Activity部分代码:
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class MyAndroidProject extends Activity { ImageView iv; Button btnNext; Button btnPrevious; Button btnAlphaPlus; Button btnAlphaMinus; int currImgId = 0; int alpha = 255; int[] imgId ={ R.drawable.pic1, R.drawable.pic2, R.drawable.pic3 }; private View.OnClickListener myListener = new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if(v == btnNext){ currImgId = (currImgId+1) %imgId.length; iv.setImageResource(imgId[currImgId]); } else if(v == btnPrevious){ currImgId = (currImgId-1+imgId.length) %imgId.length; iv.setImageResource(imgId[currImgId]); } else if(v == btnAlphaPlus){ alpha -= 25; if(alpha < 0){ alpha = 0; } iv.setAlpha(alpha); } else if(v == btnAlphaMinus){ alpha +=25; if(alpha >255){ alpha = 255; } iv.setAlpha(alpha); } } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv = (ImageView) findViewById(R.id.iv); btnNext = (Button) findViewById(R.id.next); btnPrevious = (Button) findViewById(R.id.previous); btnAlphaPlus = (Button) findViewById(R.id.alpha_plus); btnAlphaMinus = (Button) findViewById(R.id.alpha_minus); btnNext.setOnClickListener(myListener); btnPrevious.setOnClickListener(myListener); btnAlphaPlus.setOnClickListener(myListener); btnAlphaMinus.setOnClickListener(myListener); } }
3. 时钟控件
AnalogClock和DigitalClock都负责显示时钟,所不同的是AnalogClock控件显示模拟时钟,且只显示时针和分针,而digital显示数字时钟,可精确到秒
直接在main.xml中定义时钟控件即可。
4. 日期与时间选择控件
DatePicker继承自FrameLayout类,主要功能是向用户提供包含年,月,日的日期数据,并允许用户进行选择。如果要捕获用户修改日期选择控件中数据的时间,需要为DatePicker添加onDateChangedListener监听器。
TimePicker同样继承自FrameLayout类。时间选择控件向用户显示一天中的时间,并允许用户进行选择,如果要捕获用户修改时间数据的时间,便需要为TimePicker添加OnTimeChangedListener监听器。
案例:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <DatePicker android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/datePicker1"></DatePicker> <EditText android:layout_height="wrap_content" android:text="EditText" android:layout_width="fill_parent" android:id="@+id/editText1" android:cursorVisible="false" android:editable="false"></EditText> <TimePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/timePicker1" android:layout_gravity="center_horizontal"></TimePicker> <EditText android:layout_height="wrap_content" android:text="EditText" android:layout_width="fill_parent" android:id="@+id/editText2" android:cursorVisible="false" android:editable="false"></EditText> </LinearLayout>
Activity:
package qijia.si; import java.util.Calendar; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.EditText; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MyAndroidProject extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DatePicker dp = (DatePicker) findViewById(R.id.datePicker1); TimePicker tp = (TimePicker) findViewById(R.id.timePicker1); Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int monthOfYear = c.get(Calendar.MONTH); int dayOfMonth = c.get(Calendar.DAY_OF_MONTH); dp.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener(){ public void onDateChanged(DatePicker view,int year, int monthOfYear,int dayOfMonth){ flushDate(year,monthOfYear,dayOfMonth); } }); tp.setOnTimeChangedListener(new OnTimeChangedListener(){ public void onTimeChanged(TimePicker view,int hourOfDay,int minute){ flushTime(hourOfDay,minute); } }); } public void flushDate(int year,int monthOfYear,int dayOfMonth){ EditText et = (EditText) findViewById(R.id.editText1); et.setText("您选择的日期是: "+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日"); } public void flushTime(int hourOfDay,int minute){ EditText et = (EditText) findViewById(R.id.editText2); et.setText("您选择的时间是: "+hourOfDay+"时"+minute+"分"); } }
5. 动画播放技术
动画播放技术主要有两种:帧动画和补间动画。
1) 帧动画
帧动画主要用到的类是AnimationDrawable,每个帧动画都是一个AnimationDrawable对象
定义帧动画可以在代码中直接进行,也可以通过XML文件进行定义,定义帧动画的XML文件将存放在res/anim目录下。XML文件中指定了图片图片帧出现的顺序及每个帧的连续时间。
anim.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot = "false"> <item android:drawable = "@drawable/f1" android:duration="500" android:visible = "true"/> <item android:drawable = "@drawable/f2" android:duration="500" android:visible = "true"/> <item android:drawable = "@drawable/f3" android:duration="500" android:visible = "true"/> <item android:drawable = "@drawable/f4" android:duration="500" android:visible = "true"/> </animation-list>
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/linearLayout1"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView1" android:layout_gravity="center_horizontal" android:src="@anim/anim"></ImageView> <Button android:layout_height="wrap_content" android:id="@+id/button1" android:text="click" android:layout_width="fill_parent" android:layout_gravity="center_horizontal"></Button> </LinearLayout>
Activity:
package qijia.si; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MyAndroidProject extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener(){ public void onClick(View v){ ImageView iv = (ImageView) findViewById(R.id.imageView1); iv.setBackgroundResource(R.anim.anim); AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); ad.start(); } }); } }2)补间动画
补间动画作用于View对象,主要包括View对象的位置,尺寸,旋转角度和透明度的变换。
补间动画可以通过XML声明也可以在代码中动态定义。推荐使用XML因为XML文件可读性及可用性高,方便替换。
发表评论
-
Android开发06—菜单与对话框(下)
2011-05-16 19:11 1113Android开发06—菜单与对 ... -
Android开发06—菜单与对话框(上)
2011-05-13 20:07 1188Android开发06—菜单与对话框(上) 1. 菜单 1) ... -
Android开发05—Android常用高级控件(下)
2011-05-09 19:16 1210Android开发05—Android常用高级控件(下) 1 ... -
Android开发04—Android常用高级控件(上)
2011-05-04 17:38 1845Android开发04—Android常用高级控件(上) 1 ... -
Android的adapter总结和深入研究
2011-05-04 17:35 1821Android的adapter总结和深 ... -
Android开发03—Android常用基本控件(上)
2011-04-30 16:30 1191Android开发03—Android常用基本控件(上) 1 ... -
Android系统开发02—Android布局管理器
2011-04-26 19:36 983Android系统开发02—Android布局管理器 1. ... -
Android系统开发01—Android基本组件
2011-04-26 19:33 933Android系统开发01—Android基本组件 1. 应 ...
相关推荐
android 常用控件如:ProgressBar、ActionBar、Switch、ListView 、PopupWindow....等的用法,各种Style的写法以及自定义常用控件 android 五大布局的汇总及用法
教程名称: 老罗Android开发视频教程-Android常用UI控件编程【32集】【】Android常用UI控件编程第七集【】Android常用UI控件编程第二十三集【】Android常用UI控件编程第二十九集【】Android常用UI控件编程第二十二...
NULL 博文链接:https://zzqrj.iteye.com/blog/1124365
主要讲解Android发展史、Android五大布局、Android基础常用控件、Android开发工具等讲解
Android移动平台开发-实验报告
包括了所OPHONE&android所有常用控件,手机开发必备 OPHONE &android常用控件 OPHONE &android常用控件
Android 中常用的控件介绍及其相应的使用,内含实例代码
Android UI控件组件库集合【源码】 热门标签,流式布局,刮刮乐控件,侧滑控件,循环幻灯片控件,自动换行控件,下拉列表,圆角进度控件等常用UI控件组件合集
雨松MOMO带大家盘点Android 开发中的常用系统控件 欢迎大家下载阅读。。
4.2 常用控件应用 4.2.1 文本框(Textiew) 4.2.2 列表(ListView) 4.2.3 提示(T0ast) 4.2.4 编辑框(EditText) 4.2.5 单项选择(RadioGroup、RadioButton 4.2.6 多项选择(CheckBox) 4.2.7 下拉列表(Spinner) 4.2.8 自动...
Android常用控件开发详解.pdf
【第一版第八章】老罗Android开发视频-常用UI控件(33集) 【第一版第九章】老罗Android开发视频--存储数据和文件(7集) 【第一版第十章】老罗Android开发视频--对话框介绍(4集) 【第一版第十一章】老罗...
第11章 循序渐进——开发Android应用的基本步骤 11.1 兵马未动粮草先行——应用规划及架构设计 11.2 应用开发步骤 11.2.1 界面设计始终是第一位——实现UI 11.2.2 必备的动力源泉——数据操作和存储 11.2.3 华丽转身...
这里面有很多android开发中所使用的控件,并且以一种交互式的界面将其展现出来,希望能对android开发初学者有所帮助。
Android开发常用整理(不断扩充中)包含各种工具类、线程池、日志、自定义的控件、程序崩溃捕捉处理、默认的Application配置、常用的Adapter等 注意: > 如果需要使用FragmentTabAdapter,则需要android-support-v4....
现在我们公司得app都是差不多得title,所以抽时间自己撸了个常用得标题栏控件,直接在布局中使用。 不过本人还是推荐使用官方toolbar 等官方 新控件 ,效果太炫了。无奈设计要求这样,所以在这个公司干,为了开发...
《卓越工程师培养计划"十二五"规划教材:Android嵌入式应用开发》主要内容包括Android基础知识、Android应用程序开发的基本流程、Android应用程序开发的常用编程接口、常用控件及一些在应用程序中常用的模块和功能的...
RxLibrary工程:一些常用的工具类,以及常用的控件,主要用来提高开发效率
包括开发环境搭建、Android语言基础、常用布局及控件、四大组件、图形图像技术、多媒体应用、数据处理技术、触摸和手势识别、多线程、网络技术、定位、蓝牙以及VR和NDK开发等知识,《Android开发实战:从学习到产品...