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.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章