2013-06-21 35 views
12

Tôi mới sử dụng Android và học cách làm việc với TextSwitcher. Tôi muốn biết làm thế nào một văn bản có thể được Hoạt hình bằng cách sử dụng TextSwitcher.Animate Text Sử dụng TextSwitcher Trong Android

Tôi có bố cục bằng nút TextSwitcher và nút. Khi tôi bấm vào nút, TextSwitcher nên chuyển văn bản.

tôi đọc về vấn đề này ở đây ....

Create Android TextSwitcher with dynamically generated Textview

Nhưng tôi không thể làm cho nó làm việc.

Cách tạo hoạt ảnh cho Văn bản, như vậy khi tôi nhấp vào Nút TextSwitcher sẽ chuyển văn bản.

+0

mà là vấn đề – Sam

Trả lời

14

Trình chuyển đổi văn bản có thể được sử dụng để tạo hiệu ứng văn bản trên màn hình. Xem blog để xem chi tiếtUsing TextSwitcher In Androidvà tất cả các nội dung được lấy từ blogUsing TextSwitcher In Android

Chúng ta cần phải thiết lập trong và ngoài phim hoạt hình.

  1. Trong hoạt ảnh: Văn bản nào xuất hiện trên màn hình.
  2. Hoạt ảnh ngoài: Văn bản nào xuất hiện từ Màn hình.

Full Mã với ý kiến ​​đúng đắn

public class MainActivity extends Activity { 
    private TextSwitcher mSwitcher; 
    Button btnNext; 

    // Array of String to Show In TextSwitcher 
    String textToShow[]={"Main HeadLine","Your Message","New In Technology","New Articles","Business News","What IS New"}; 
    int messageCount=textToShow.length; 
    // to keep current Index of text 
    int currentIndex=-1; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.example1_layout); 

     // get The references 
     btnNext=(Button)findViewById(R.id.buttonNext); 
     mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher); 

     // Set the ViewFactory of the TextSwitcher that will create TextView object when asked 
     mSwitcher.setFactory(new ViewFactory() { 

      public View makeView() { 
       // TODO Auto-generated method stub 
       // create new textView and set the properties like colour, size, gravity etc 
       TextView myText = new TextView(MainActivity.this); 
       myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL); 
       myText.setTextSize(36); 
       myText.setTextColor(Color.BLUE); 
       return myText; 
      } 
     }); 

     // Declare the in and out animations and initialize them 
     Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); 
     Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); 

     // set the animation type of textSwitcher 
     mSwitcher.setInAnimation(in); 
     mSwitcher.setOutAnimation(out); 

     // ClickListener for NEXT button 
     // When clicked on Button TextSwitcher will switch between texts 
     // The current Text will go OUT and next text will come in with specified animation 
     btnNext.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       currentIndex++; 
       // If index reaches maximum reset it 
       if(currentIndex==messageCount) 
        currentIndex=0; 
       mSwitcher.setText(textToShow[currentIndex]); 
      } 
     }); 

    } 
} 
Các vấn đề liên quan