2013-01-03 2 views
0

R.anim.fade_in 및 out을 사용하여 TextSwitcher를 설정했습니다. 단추를 클릭하면 텍스트가 표시되고, 다음 번에는 텍스트가 보이지 않습니다 (페이드 아웃과 같은). 다음 클릭 텍스트는 다시 확인됩니다. 테스트는 표시되지 않습니다. 내 오류 어디 있어요? 여기textSwitcher with fade

mSwitcher = (TextSwitcher) findViewById(R.id.switcher); 
     mSwitcher.setFactory(this); 

     Animation in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in); 
     Animation out = AnimationUtils.loadAnimation(this,android.R.anim.fade_out); 
     mSwitcher.setInAnimation(in); 
     mSwitcher.setOutAnimation(out); 

mSwitcher.setText(""+prog[x]); 
+0

무엇이 prog [x]입니까? – deadfish

+0

배열 : prog [0] = "one", prog [1] = "two" –

+0

x 색인이 범위를 벗어나는 경우 어떻게됩니까? – deadfish

답변

1

문자열의 배열로부터 적당한 요소를 얻을 ViewFactory의 안전 incremeneting 카운터를 구현 fix 가진 단순한 예이다.

홈페이지 :

public class MainActivity extends Activity implements OnClickListener, ViewFactory { 

    private TextSwitcher mSwitcher; 
    private int counter = 0; 
    private String[] words = new String[]{"one","two","three"}; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mSwitcher = (TextSwitcher) findViewById(R.id.textswitcher); 
     mSwitcher.setFactory(this); 

     Animation in = AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_in); 

     Animation out = AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_out); 
     mSwitcher.setInAnimation(in); 
     mSwitcher.setOutAnimation(out); 

     Button nextButton = (Button) findViewById(R.id.next); 
     nextButton.setOnClickListener(this); 

     updateCounter(); 
    } 

    public void onClick(View v) { 
     counter++; 
     updateCounter(); 
    } 

    private void updateCounter() { 
     int index = counter % words.length; 
     mSwitcher.setText(String.valueOf(words[index])); 
    } 

    public View makeView() { 
     TextView t = new TextView(this); 
     t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); 
     t.setTextSize(36); 
     return t; 
    } 
} 

와 XML :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <TextSwitcher 
     android:id="@+id/textswitcher" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <Button 
     android:id="@+id/next" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="next" /> 

</LinearLayout> 

당신이 요소의 배열에 범위를 벗어난와 요소를 호출 할 경우 잘 작동하지 않습니다 표시 ...에주의하시기 바랍니다 그것.