更多精彩内容,请点击阅读:《API Demos 2.3 学习笔记》
下面简单介绍怎么创建和使用TextSwitcher控件。
1、首先,在布局文件中定义一个TextSwitcher控件
<TextSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
2、创建 Activity时,同时继承ViewSwitcher.ViewFactory接口。
public class TextSwitcher1 extends Activity implements ViewSwitcher.ViewFactory,
3、通过findViewById 实例化TextSwitcher对象
TextSwitcher mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
4、通过如下设置。每次TextSwitcher的setText(CharSequence)方法 ,都会刷新页面,并调用 makeView来生成一个TextView对象,用于显示文字。
mSwitcher.setFactory(this);
//每次调用TextSwitcher的setText方法,UI主线程会调用该函数来生成一个TextView,用于显示文字。
//这里可以加上自定义属性来自定义TextView。例如,我设置了文字的颜色为蓝色。
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextColor(Color.BLUE);
t.setTextSize(36);
return t;
}
//加载并设置TextSwitcher的淡入淡出动画
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
6、最后,调用setText(CharSequence)方法 刷新界面。刷新流程参考第4条。
mSwitcher.setText(String.valueOf(mCounter));
下面我们进行实例代码解析:
res-layout-text_switcher_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 一个Button对象 -->
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_switcher_1_next_text" />
<!-- 一个TextSwitcher对象 -->
<TextSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
src-com.example.android.apis.view-TextSwitcher1.java
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
/**
* 演示如何使用 TextSwitcher 控件
*/
public class TextSwitcher1 extends Activity implements ViewSwitcher.ViewFactory,
View.OnClickListener {
private TextSwitcher mSwitcher;
private int mCounter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_switcher_1);
// 通过findViewById获得一个TextSwitcher对象
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
//加载并设置TextSwitcher的淡入淡出动画
Animation in = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
mSwitcher.setInAnimation(in);
mSwitcher.setOutAnimation(out);
// 通过findViewById获得一个Button对象,并设置监听器
Button nextButton = (Button) findViewById(R.id.next);
nextButton.setOnClickListener(this);
updateCounter();
}
//Button的监听器实现代码。
//每次点击按钮,mCounter就会加上1,并且刷新界面
public void onClick(View v) {
mCounter++;
updateCounter();
}
//每次调用TextSwitcher的setText方法,都会刷新界面。
//用淡出动画效果隐藏旧的TextView,接着用淡入效果加载新的TextView
private void updateCounter() {
mSwitcher.setText(String.valueOf(mCounter));
}
//每次调用TextSwitcher的setText方法,UI主线程会调用该函数来生成一个TextView,用于显示文字。
//这里可以加上自定义属性来自定义TextView。例如,我设置了文字的颜色为蓝色。
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextColor(Color.BLUE);
t.setTextSize(36);
return t;
}
}
预览效果: