TextSwitcher 字面理解是文字交换器,是ViewSwitcher的子类,从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,显然是交换TextView。

效果图:


应用分为三步:
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));

代码:

Java代码
  1. packagecom.zhou.activity;
  2. importjava.util.Random;
  3. importandroid.app.Activity;
  4. importandroid.os.Bundle;
  5. importandroid.view.View;
  6. importandroid.view.animation.Animation;
  7. importandroid.view.animation.AnimationUtils;
  8. importandroid.widget.Button;
  9. importandroid.widget.TextSwitcher;
  10. importandroid.widget.TextView;
  11. importandroid.widget.ViewSwitcher;
  12. publicclassTextSwitcherActivityextendsActivityimplementsViewSwitcher.ViewFactory{
  13. @Override
  14. protectedvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. this.setContentView(R.layout.textswithcer);
  17. //设置标题
  18. setTitle("文字转换器");
  19. //取得文字转换器
  20. finalTextSwitcherswitcher=(TextSwitcher)findViewById(R.id.textSwitcher);
  21. //指定转换器的ViewSwitcher.ViewFactory,ViewSwitcher.ViewFactory会为TextSwitcher提供转换的View
  22. switcher.setFactory(this);
  23. //如果不用switcher.setFactory()方法设置转换时的View,也可以调用两次switcher.addView(view,index,params);
  24. //其中view为要切换的View,index为索引,params是添加时的宽,高参数
  25. //TextViewtextView1=newTextView(this);
  26. //textView1.setTextSize(36);
  27. //textView1.setTextColor(Color.RED);
  28. //TextViewtextView2=newTextView(this);
  29. //textView2.setTextSize(36);
  30. //textView2.setTextColor(Color.YELLOW);
  31. //switcher.addView(textView1,0,newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  32. //switcher.addView(textView2,1,newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  33. //设置转换时的淡入和淡出动画效果(可选)
  34. Animationin=AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
  35. Animationout=AnimationUtils.loadAnimation(this,android.R.anim.fade_out);
  36. switcher.setInAnimation(in);
  37. switcher.setOutAnimation(out);
  38. //单击一次按钮改变一次文字
  39. ButtonbtnChange=(Button)this.findViewById(R.id.btnChange);
  40. btnChange.setOnClickListener(newView.OnClickListener(){
  41. @Override
  42. publicvoidonClick(Viewv){
  43. //为TextSwitcher设置显示内容,执行一次switcher.setText()方法,就会切换到下一个View
  44. switcher.setText(String.valueOf(newRandom().nextInt()));
  45. }
  46. });
  47. }
  48. //重写ViewSwitcher.ViewFactory的makeView()方法,返回一个View,TextSwitcher交换时使用
  49. @Override
  50. publicViewmakeView(){
  51. TextViewtextView=newTextView(this);
  52. textView.setTextSize(36);
  53. returntextView;
  54. }
  55. }

ps: 关于如何更改TextSwitcher字体颜色的问题

这个问题咋一看简单,但是没有门路的话,半天也解决不了,我也遇到了这个问题,我的TextSwitcher默认颜色是灰色,和我的背景图颜色差不多了,想改个颜色,但是找了很久也找不到解决办法。

弄了一小时才找到解决的办法(还要感谢某群的群主“飞雪无情”)给我的提示~~

现特贴出解决方案(其实很简单,但是一时想不到的话,也会让人很抓狂):

Java代码
  1. publicViewmakeView(){
  2. TextViewtv=newTextView(this);
  3. tv.setTextSize(36);
  4. tv.setTextColor(Color.BLACK);
  5. returntv;
  6. }

修改TextSwitcher的makeView()中的 tv.setTextColor(Color.BLACK); 就好了。


更多相关文章

  1. RK3288 android7.1.2 插 UVCCamera 摄像头, android studio 调试9
  2. Android中Intent的几种使用方法
  3. android初中高开发工程师必需要装的一个APP
  4. android 按两次返回键退出程序
  5. javascript 与 webview 交互
  6. android MediaPlayer+Stagefright架构(音频)图解
  7. Android(安卓)软件自动更新功能实现的方法
  8. Android对接第三方登录,微信登录
  9. Android中使用Gesture加入手势操作

随机推荐

  1. Android(安卓)调用网易微博开放API
  2. Android全屏显示 无标题栏、全屏、设置为
  3. Android——IntentFilter匹配规则
  4. Android(安卓)Wifi 的电源管理
  5. Android非常强大的第三方数据库LitePal
  6. SAX解析XML文件
  7. 从数据库中获取浏览器书签信息(Android Br
  8. Android Android使用JSON与服务器交互
  9. Android Settings中快速搜索流程
  10. Android keyevent 中的各个值