TextSwitcher使用示例
mainActivity如下:
package c.c;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AnimationUtils;import android.widget.TextSwitcher;import android.widget.TextView;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity { private TextSwitcher mIextSwitcher; private String texts []; private int textIndex=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ texts=new String[]{"空山新雨后","天气晚来秋","明月松间照","清泉石上流"}; mIextSwitcher=(TextSwitcher) findViewById(R.id.textSwitcher); //设置用于显示文字的TextView mIextSwitcher.setFactory(new ViewFactory() {public View makeView() {TextView textView=new TextView(MainActivity.this);textView.setTextSize(30);return textView; } }); //处理点击事件 mIextSwitcher.setOnClickListener(new ClickListenerImpl()); //设置文字进入和退出动画 mIextSwitcher.setInAnimation (AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left)); mIextSwitcher.setOutAnimation (AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left)); //设置初始时显示的文字 mIextSwitcher.setText(texts[textIndex]); }private class ClickListenerImpl implements OnClickListener {public void onClick(View v) {textIndex++;if (textIndex == texts.length) {textIndex = 0;} mIextSwitcher.setText(texts[textIndex]);}}}
main.xml如下:
<RelativeLayout 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" > <TextSwitcher android:id="@+id/textSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /></RelativeLayout>