TextSwitcher的用法

 

  
  
  
  
  1. public class MainActivity extends Activity implements ViewSwitcher.ViewFactory, View.OnClickListener{ 
  2.     int count = 0
  3.     private TextSwitcher textSwitcher; 
  4.     @Override 
  5.     protected void onCreate(Bundle savedInstanceState) { 
  6.         super.onCreate(savedInstanceState); 
  7.         setContentView(R.layout.activity_main); 
  8.         setTitle("文字转换器"); 
  9.          
  10.         Button button=(Button) findViewById(R.id.button1); 
  11.         textSwitcher=(TextSwitcher) findViewById(R.id.textSwitcher1); 
  12.         //指定转换器的viewSwitcher.viewFactory,提供转换所用的view 
  13.         textSwitcher.setFactory(this); 
  14.         //如果不用switcher.setFactory()方法设置转换时的View,也可以调用两次switcher.addView(view,index,params);    
  15.         /*TextView textView1=new TextView(this); 
  16.         TextView textView2=new TextView(this); 
  17.         textSwitcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
  18.         textSwitcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));*/ 
  19.         //为view转换时设置动画(可选) 
  20.         Animation animation1=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim1); 
  21.         Animation animation2=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim2); 
  22.         textSwitcher.setInAnimation(animation1); 
  23.         textSwitcher.setOutAnimation(animation2); 
  24.          
  25.         button.setOnClickListener(this); 
  26.         setCount(); 
  27.     } 
  28.     //重写ViewSwitcher.ViewFactory的makeView方法,返回一个view作转换时用 
  29.     @Override 
  30.     public View makeView() { 
  31.         // TODO Auto-generated method stub 
  32.         TextView textView=new TextView(MainActivity.this); 
  33.         textView.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL); 
  34.         return textView; 
  35.     } 
  36.      
  37.     @Override 
  38.     public void onClick(View v) { 
  39.         // TODO Auto-generated method stub 
  40.         count++; 
  41.         setCount(); 
  42.     } 
  43.     private void setCount() { 
  44.         // TODO Auto-generated method stub 
  45.         textSwitcher.setText(String.valueOf(count)); 
  46.     } 

 

你可能感兴趣的:(android,TextSwitcher的用法)