TextSwitcher 는 Text를 변경하는데 사용하는 View입니다.
Text를 변경할 때 효과를 줄 수 있다는 장점이 있습니다.

다음 예제는 Apidemos에 나온 것과 동일한 내용을 다룹니다.
다른 점은 ApiDemos는 버튼에 반응하지만, 이 예제는 TextSwitcher를 한번 클릭할 때 반응합니다.

1. 기본 프로젝트를 생성합니다.
2. main.xml의 내용을 아래와 같이 수정합니다.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextSwitcher android:id="@+id/time_switcher"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content" android:layout_gravity="center_horizontal"/>  
  10. </LinearLayout>  

3. 소스 파일을 열고 아래와 같이 코딩합니다.
  1. package com.sohon.app.dynamicWP;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.Gravity;   
  6. import android.view.View;   
  7. import android.view.View.OnClickListener;   
  8. import android.view.animation.Animation;   
  9. import android.view.animation.AnimationUtils;   
  10. import android.widget.TextSwitcher;   
  11. import android.widget.TextView;   
  12. import android.widget.ViewSwitcher.ViewFactory;   
  13.   
  14. public class ActContent extends Activity implements ViewFactory {   
  15.     private TextSwitcher timeSwitcher;   
  16.   
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {   
  20.         super.onCreate(savedInstanceState);   
  21.         setContentView(R.layout.main);   
  22.            
  23.         timeSwitcher = (TextSwitcher) findViewById(R.id.time_switcher);   
  24.         timeSwitcher.setFactory(this);   
  25.         timeSwitcher.setOnClickListener(new OnClickListener() {   
  26.             public void onClick(View v) {   
  27.                 ActContent.this.nextText();   
  28.             }   
  29.         });   
  30.         Animation in = AnimationUtils.loadAnimation(this,   
  31.                 android.R.anim.fade_in);   
  32.         Animation out = AnimationUtils.loadAnimation(this,   
  33.                 android.R.anim.fade_out);   
  34.         timeSwitcher.setInAnimation(in);   
  35.         timeSwitcher.setOutAnimation(out);   
  36.         timeSwitcher.setText("1초");   
  37.            
  38.     }   
  39.   
  40.     protected void nextText() {   
  41.         timeSwitcher.setText("2초");   
  42.     }   
  43.   
  44.     public View makeView() {   
  45.         TextView t = new TextView(this);   
  46.         t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);   
  47.         t.setTextSize(36);   
  48.         return t;   
  49.     }   
  50. }  

timeSwitcher.setFactory(this); 라는 문장은 매우 중요합니다.
이게 없으면  java.lang.NullPointerException 가 발생합니다.
setFactory를 추가하면 ViewFactory를 구현할 것을 요구합니다.

여기에 추가하는 View를 이용해 TextView를 생성하는 것 같습니다.

ps> 이런게 있는 줄 알았다면 좀 더 쉽게 만들었을 것을 이라고 뒤늦게 후회해보고 있습니다.

+ Recent posts