`

lost android 开发教程二

 
阅读更多
第二季课程介绍
1、控件使用方法介绍
   Sprinner,AutoCompleteTextView,RatingBar,SeekBar等等;
2、Widget的使用方法
3、Animation使用方法
4、常见数据解析技术

什么是Spinner
Spinner构造下拉菜单组件
创建一个Spinner的步骤
1、在布局文件中声明:
引用
<Spinner android:id="@+id/spinnerId"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             />

2、在string.xml当中声明一个数组:
引用
<string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
    </string-array>

3、创建一ArrayAdapter:
引用
ArrayAdapter<CharSequence> adapter =
        ArrayAdapter.createFromResource(
        this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

4、得到Spinner对象,并设置数据:
spinner = (Spinner) findViewById(R.id.spinnerId);
        spinner.setAdapter(adapter);
        spinner.setPrompt("测试");
创建监听器
    class SpinnerOnSelectedListener implements OnItemSelectedListener {
		@Override
		public void onItemSelected(AdapterView<?> parent, 
		        View view, int pos, long id) {
			Toast.makeText(parent.getContext(), "The planet is " + 
	          parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show(); 
		}
		@Override
		public void onNothingSelected(AdapterView<?> parent) {
           System.out.println("onNothingSelected");			
		}
    }

绑定监听器
spinner.setOnItemSelectedListener(new SpinnerOnSelectedListener());
ArrayAdapter的另一种用法
除了从使用strings.xml文件当中的数组创建ArrayAdapter外可以动态创建Arraydapter
引用
List<String> list = new ArrayList<String>();
        list.add("list1");
        list.add("list2");
  ArrayAdapter<CharSequence> adapter =  new ArrayAdapter(this,R.layout.item,R.id.textStringiewld,list);


常用控件
DatePicker和DatePickerDialog的基本使用方法
什么是DatePicker
android内置日期控件
创建DatePickerDialog的步骤
1、声明一个监听器,使用匿名内部类:
引用
OnDateSetListener onDateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
System.out.println(year + "-" + monthOfYear + dayOfMonth);
}
};

2、复写onCreateDialog(int id)方法:
@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_PICKER_ID:
			return new DatePickerDialog(this,onDateSetListener,
					2012,02,20); //月份从0开始的,这里指的是三月
		}
		return null;
	}

3、在需要的时候调用showDialog方法:
showDialog(DATE_PICKER_ID);

AutoCompleteTextView的基本使用方法
自动查找输入文本框
1、在布局文件当中声名一个AutoCompleteTextView
引用
<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

2、定义提示条目的样式,在res/layout文件夹下新建一个布局文件,名为list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="10dp"
  android:textSize="16sp"
  android:text="#000"
  >
</TextView>

3、创建一个ArrayAdapter AuotoCommpleteTextView需要使用ArrayAdapter来提供数据:
它即可以用List,也可用数组作为数据
引用
List<String> list = new ArrayList<String>();
        list.add("list1");
        list.add("list2");
   ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this,R.layout.list_item,list);

4、为AuotCompleteTextView设置数据
autoCompleteTextView.setAdapter(adapter);

ExpandableListActivity的基本使用方法
什么是ExpandableListActivity
列表组分组显示出来

seekBar的基本使用方法
进度 条带滑块进度条
RatingBar的基本使用方法
评级五角星工具条

App Widget基本使用方法
1、AppWidgetProviderInfo对象:
为App Widget提供元数据,包括布局,更新频率等等数据。这个对象被定义在XML文件当中;
2、AppWidgetProvider:
定义了App Widget的基本生命周期函数
创建第一个App Widget的步骤
1、定义AppWidgetProviderInfo:
在res/xml文件夹当中定义一个名为example_appwidget_info.xml的文件:

2、为App Widget指定样式和布局:

3、实现AppWidgetProvider
onUpdate:在到达指定的更新时间之后或者当用户向桌面添加App Widget时调用该方法;
onDeleted:当App Widget被删除时,会调用该方法;
onEnabled:当一个App Widget的实例第一次被创建时,会调用该方法;
onDisabled:当最后一个App Widget实例被删除后,会调用该方法;
onReveice:接收广播事件

PendingIntent的作用
进程A交给进程B,进程B遇到事件后执行
创建的PendingIntent方法

RemoteViews的作用
1、RemoteView  表示一系列的View对象
2、RemoteView 表示的对象运行在另外的进程当中

1、接受来自AppWidget的广播
Intent intent = new Intent();
intent.setAction(UPDATE_ACTION);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
remoteViews.setOnClickPendingIntent(R.id.appWidgetbutton, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

2、更新AppWidget当中控件的状态
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.example_appwidget);
//remoteViews.setImageViewResource(R.id.appWidgetbutton, R.drawable.icon);
remoteViews.setTextViewText(R.id.appWidgetTextId, "text");
  AppWidgetManager appWidgetManager =
  AppWidgetManager.getInstance(context);
ComponentName componentName = new ComponentName(context, 
  ExampleAppwidgetProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteViews);

Animations的使用
什么是Animations
Animations是实现动画的API,提供了系列的动画效果,这些效果可以应用在绝大多数控件
Animations从总体来说可以分为两大类
第一类:TweenedAnimations
该类Animations提供了旋转、移动、伸展和淡出等等效果
1、Alpha:淡入浅出效果
2、Scale:缩放效果
3、Rotate:旋转效果
4、Translate:移动效果

第二类:Frame-by-Frame Animations
这一类Animations可以创建一个Drammwable序列,这些Drawable可以按照指定的时间间歇一个一个显示

使用Tweened Animations的步骤
1、创建一个AnimationSet对象;
2、根据需要创建相应的Animation对象;
3、根据软件动画的需求,为Animation对象设置相应的数据;
4、将Animation对象添加到AnimationSet对象当中;
5、使用控件对象开始执行AnimationSet;
Tweened Animations的通用属性
1、setDuration (long durationMillis)
设置动画持续时间
2、setFillAfter(boolean fillAfter)
如果fillAfter的值为true;则动画执行后,控件将停留在执行结束的状态;
3、setFillBefore(boolean fillBefore)
如果fillAfter的值为true;则动画执行后,控件将回到动画执行之前的状态;
4、setStartOffset(long startOffset)
设置动画执行之前的等待时间;
5、setRepeatCount(int repeatCount)
设置动画重复执行次数;

Animations的第二种使用方法(xml可利用性高)
1、在res文件下面新建一个名为anim的文件夹;
2、创建xml文件,并首先加入set标签,改标签如下:
引用
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_interpolator">
</set>

3、在该标签当中加入rotate,alpha,scale或者translate标签;
4、在代码当中使用AnimationUtils当中装载xml文件,并生成或Animation圣像;
Alpha 的xml文件编写方法
引用
<alpha android:fromAlpha="0.1"
       android:toAlpha="1.0"
       android:duration="3000" />
Rotate 的xml文件编写方法
<rotate android:fromDegrees ="0"
        android:toDegrees="+350"
android:pivotX = "50%"
android:pivotY = "50%"
android:duration="3000" />

android:pivotX的值共有三种设置方法:
1、android:pivotX="50"这种方法使用绝对位置定位
2、android:pivotX="50%"这种方法相对于控件本身定位
3、android:pivotX="50%"这种方法相对于控件的父控件定位
Translate 的xml文件编写方法
引用
<translate android:fromXDelta = "50%"
           android:toXDelta="100%"
   android:fromYDelta="0%"
   android:toYDelta="100%"
   android:duration="2000" />
Scale 的xml文件编写方法
<scale android:fromXScale="1.0"
       android:toXScale="0.0"
       android:fromYScale="0.1"
       aandroid:toYScale="0.0"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="2000" />


1、AnimationSet的使用方法
什么是AnimationSet
1、AnimationSet是Animation的子类;
2、一个AnimationSet包含了一系列的Animation
3、针对AnimationSet设置一些Animation的常见属性(如start,duration等等),可以被包含在AnimationSet当中的Animation集成;
2、Interpolator的使用方法
什么是Interpolator
Interpolator定义动画变化的速率,在Animations框架当中定义了以下几种Interpolator
AccelerateDecelerateInterpolator:在动画开始与结束的地方速度比较慢,在中间的时候加速
AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
cycleInterpolator:动画循环播放特定的次数,速率改变没着正弦曲线
DecelelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator:在动画的以均匀的速率改变
3、Frame-By-Frame Animations的使用方法
在res/drawable当中创建一个XML文件,用于定义Animations的动画序列:
<?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/p001" android:duration="500" />
   <item android:drawable="@drawable/p002" android:duration="500" />
   <item android:drawable="@drawable/p003" android:duration="500" />
</animation-list>


1、LayoutAnimationController的使用方法
什么是LayoutAnimationController
1、layoutAnimationCotroller用于为一个layout里面的控件,或者是一个ViewGrop里面的控件设置动画效果;
2、每一个控件都有相同的动画效果
3、这些控件的动画效果在不同的实现展现出来
4、LayoutAnimationController可以在xml文件当中设置,也可以在代码当中进行设置。
在XML当中使用LayoutAnimationController
1、在res/anim文件夹中创建一个新文件,名为list_anim_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
  android:delay="0.5"
  android:animationOrder="random"
  android:animation="@anim/list_anim" />

2、在布局文件当中为ListView添加如下配制:
android:layoutAnimation="@anim/list_anim_layout"
在代码当中使用LayoutAnimationController
1、创建一个Animation对象:
   可以通过装载xml文件,或者直接使用Anmation的构造函数创建Animation对象;
2、使用如下代码创建LayoutAnimatinController对象:
   LayoutAnimationCotroller lac = new LayoutAnimatincontroller(animation);
3、设置控件显示的顺序:
   lac.stOrder(layoutAnimationController.ORDER_NORMAL);
4、为ListView设置layoutAnimationController属性:
   listView.setLayoutAnimation(lac);

2、AnimationListener的使用方法
什么是AnimationListener?
1、AnimationListenter是一个监听器;
2、该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;
3、主要包括以下三个方法:
引用
1、onAnimationEnd(Animation animation)
2、onAnimationRepeat(Animation animation)
3、onAnimationStart(Animation animation)


JSON数据解析
1、什么是JSON
1、JOSN:JavaScript Object Notation;
2、JOSN数据是一系列键值对的集合;

2、JSON数据格式的特点
体积少,使用方便
描述差,不易理解

3、使用GSON解析JSON数据
官方网站http://code.google.com/p/google-gson/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics