TextSwitcher 使用详解

来源:互联网 发布:svn 默认端口号 编辑:程序博客网 时间:2024/04/27 08:49

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. package com.zhou.activity;   
  2.   
  3. import java.util.Random;   
  4.   
  5. import android.app.Activity;   
  6. import android.os.Bundle;   
  7. import android.view.View;   
  8. import android.view.animation.Animation;   
  9. import android.view.animation.AnimationUtils;   
  10. import android.widget.Button;   
  11. import android.widget.TextSwitcher;   
  12. import android.widget.TextView;   
  13. import android.widget.ViewSwitcher;   
  14.   
  15. public class TextSwitcherActivity extends Activity implements ViewSwitcher.ViewFactory{   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {   
  18.         super.onCreate(savedInstanceState);   
  19.         this.setContentView(R.layout.textswithcer);   
  20.         //设置标题   
  21.         setTitle("文字转换器");   
  22.         //取得文字转换器   
  23.         final TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);   
  24.         // 指定转换器的 ViewSwitcher.ViewFactory,ViewSwitcher.ViewFactory会为TextSwitcher提供转换的View  
  25.         switcher.setFactory(this);   
  26.            
  27.         //如果不用switcher.setFactory()方法设置转换时的View,也可以调用两次switcher.addView(view,index,params);  
  28.         //其中view为要切换的View,index为索引,params是添加时的宽,高参数  
  29. //      TextView textView1 = new TextView(this);   
  30. //      textView1.setTextSize(36);   
  31. //      textView1.setTextColor(Color.RED);   
  32. //      TextView textView2 = new TextView(this);   
  33. //      textView2.setTextSize(36);   
  34. //      textView2.setTextColor(Color.YELLOW);   
  35. //      switcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));  
  36. //      switcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));  
  37.            
  38.         // 设置转换时的淡入和淡出动画效果(可选)   
  39.         Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);   
  40.         Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);   
  41.         switcher.setInAnimation(in);   
  42.         switcher.setOutAnimation(out);   
  43.   
  44.         // 单击一次按钮改变一次文字   
  45.         Button btnChange = (Button) this.findViewById(R.id.btnChange);   
  46.         btnChange.setOnClickListener(new View.OnClickListener() {   
  47.             @Override  
  48.             public void onClick(View v) {   
  49.                 //为TextSwitcher设置显示内容,执行一次switcher.setText()方法,就会切换到下一个View  
  50.                 switcher.setText(String.valueOf(new Random().nextInt()));   
  51.             }   
  52.         });   
  53.     }   
  54.     // 重写 ViewSwitcher.ViewFactory 的 makeView()方法,返回一个 View,TextSwitcher 交换时使用  
  55.     @Override  
  56.     public View makeView() {   
  57.         TextView textView = new TextView(this);   
  58.         textView.setTextSize(36);   
  59.         return textView;   
  60.     }   
  61. }  

 

 

 

 

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

 

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

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

 

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

 

Java代码  收藏代码
  1. public View makeView() {     
  2.       TextView tv = new TextView(this);     
  3.       tv.setTextSize(36);    
  4.       tv.setTextColor(Color.BLACK);      
  5.       return tv;     
  6.   }   

 

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

 

0 0
原创粉丝点击