Textswitcher is used to make the label on the screen produce an animated effect. When the settext () method is called, textswitcher disappears the current text content and displays the new text content in the form of an animation.
1. Common Methods
Setcurrenttext (charsequence text)
Set the text content of the currently displayed Text View (non-animated display)
Settext (charsequence text)
Set the text content of the next view and switch to the next view. You can exit the current text to display the next text.
2. Instance
The layout file XML is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <button Android: id = "@ ID/btnbasetextswitcher" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Click variable Text" Android: hint = "convert text in animated form"/> <textswitcher Android: Id = "@ ID/tsbase" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </linearlayout>
The Code is as follows:
Import Java. util. random; import android. app. activity; import android. OS. bundle; import android. view. gravity; import android. view. view; import android. view. animation. animation; import android. view. animation. animationutils; import android. widget. button; import android. widget. textswitcher; import android. widget. textview; import android. widget. viewswitcher; import CN. youtous. r; public class basetextswitcher extends activity {private button btnbase; private textswitcher tsbase; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. setcontentview (R. layout. base_textswitcher); btnbase = (button) findviewbyid (R. id. btnbasetextswitcher); btnbase. setonclicklistener (New basetextswitcherclicklistener (); tsbase = (textswitcher) findviewbyid (R. id. tsbase); tsbase. setfactory (New basetextswitcherfactory (); setupanimation ();}/*** set the animation format */private void setupanimation () {animation in = animationutils. loadanimation (this, android. r. anim. slide_in_left); In. setduration (2000); // to highlight the animation effect, we set the animation event to 2 seconds for animation out = animationutils. loadanimation (this, android. r. anim. slide_out_right); out. setduration (2000); tsbase. setinanimation (in); tsbase. setoutanimation (out);}/*** viewfactory */class basetextswitcherfactory implements viewswitcher. viewfactory {@ overridepublic view makeview () {textview T = new textview (basetextswitcher. this); T. setgravity (gravity. top | gravity. center_horizontal); T. settextsize (36); Return t ;}/ *** Click Event listener */class basetextswitcherclicklistener implements view. onclicklistener {@ overridepublic void onclick (view v) {random Rand = new random (); tsbase. settext (string. valueof (Rand. nextint ()));}}}
We can note that if you only set viewfactory for textswitcher and do not set the animation, textswitcher simply replaces the text.