Package com.gc.textswitcherdemo;/* * Text switcher (textswitcher): * 1, Textswitcher inherits Viewswitcher, so it has the same characteristics as Viewswitcher * You can use the animation effect when you switch the view component. Similar to Imageswitcher, the use of Textswitcher * also requires setting up a viewfactory. Unlike Imageswitcher, the Makeview () method required by Textswitcher must return a TextView component. * 2, Textswitcher and TextView function a bit similar, they can be used to display text content, the difference is that * textswitcher effect is more dazzling, it can specify the text switch animation effect * */import Android.os.Bundle; Import Android.app.activity;import android.graphics.color;import Android.view.menu;import Android.view.View;import Android.widget.textswitcher;import Android.widget.textview;import android.widget.viewswitcher.viewfactory;/** * * @ Author Android General * */public Class Mainactivity extends Activity {private textswitcher textswitcher;private string[] Strs=n EW string[]{"Android General", "General iOS", "General Cocos2d-x", "General"};p rivate int curstr; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextSwitcher= ( Textswitcher) Findviewbyid (r.id.textswitchER); textswitcher.setfactory (new Viewfactory () {@Overridepublic View Makeview () {TextView tv=new TextView ( Mainactivity.this); Tv.settextsize (+); Tv.settextcolor (Color.magenta); return TV;}}); /Call the next method to display the next string next (null);} event handler that controls the display of the next string public void Next (View source) {Textswitcher.settext (strs[curstr++%strs.length]);}}
The layout file is as follows:
<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 " android:orientation= "vertical" > <!--defines a textswitcher and specifies the animation effect when text is toggled- < Textswitcher android:id= "@+id/textswitcher" android:layout_width= "match_parent" android:layout_ height= "Wrap_content" android:inanimation= "@android: Anim/slide_in_left" android:outanimation= "@ Android:anim/slide_out_right " android:onclick=" Next " /> </LinearLayout>
Effect:
Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/25604177