Android TextSwitcher(文字交换器)的使用

TextSwitcher 字面理解是文字交换器,是ViewSwitcher的子类,从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,显然是交换TextView。
效果图:
[img]http://dl.iteye.com/upload/attachment/372120/7d2f7592-57f6-3aa5-9ae5-54f5150ed5d5.png[/img]
[img]http://dl.iteye.com/upload/attachment/372122/b80ff2a6-9859-3532-870f-ec06fc16fd59.png[/img]
应用分为三步:
1.得到 TextSwitcher 实例对象
TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
2.为switcher指定ViewSwitcher.ViewFactory工厂,该工厂会产生出转换时需要的View
switcher.setFactory(this);
3.为switcher设定显示的内容,该方法执行,就会切换到下个View
switcher.setText(String.valueOf(new Random().nextInt()));

其中 要实现ViewSwitcher.ViewFactory中的makeView()方法
// 重写 ViewSwitcher.ViewFactory 的 makeView()方法,返回一个 View,TextSwitcher 交换时使用
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(36);
return textView;
}

如果不适用ViewSwitcher.ViewFactory,也可以使用下面的方式代替
//如果不用switcher.setFactory()方法设置转换时的View,也可以调用两次switcher.addView(view,index,params);
//其中view为要切换的View,index为索引,params是添加时的宽,高参数
// TextView textView1 = new TextView(this);
// textView1.setTextSize(36);
// textView1.setTextColor(Color.RED);
// TextView textView2 = new TextView(this);
// textView2.setTextSize(36);
// textView2.setTextColor(Color.YELLOW);
// switcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// switcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

代码:

package com.zhou.activity;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
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;

public class TextSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.textswithcer);
//设置标题
setTitle("文字转换器");
//取得文字转换器
final TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
// 指定转换器的 ViewSwitcher.ViewFactory,ViewSwitcher.ViewFactory会为TextSwitcher提供转换的View
switcher.setFactory(this);

//如果不用switcher.setFactory()方法设置转换时的View,也可以调用两次switcher.addView(view,index,params);
//其中view为要切换的View,index为索引,params是添加时的宽,高参数
// TextView textView1 = new TextView(this);
// textView1.setTextSize(36);
// textView1.setTextColor(Color.RED);
// TextView textView2 = new TextView(this);
// textView2.setTextSize(36);
// textView2.setTextColor(Color.YELLOW);
// switcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// switcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

// 设置转换时的淡入和淡出动画效果(可选)
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
switcher.setInAnimation(in);
switcher.setOutAnimation(out);

// 单击一次按钮改变一次文字
Button btnChange = (Button) this.findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//为TextSwitcher设置显示内容,执行一次switcher.setText()方法,就会切换到下一个View
switcher.setText(String.valueOf(new Random().nextInt()));
}
});
}
// 重写 ViewSwitcher.ViewFactory 的 makeView()方法,返回一个 View,TextSwitcher 交换时使用
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(36);
return textView;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值