<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ethan.activity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".TextSwitcherActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextSwitcher
android:id="@+id/textSwitcher1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextSwitcher>
</LinearLayout>
package com.ethan.activity;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
public class TextSwitcherActivity extends Activity {
// 索引
private int index;
// 文本数组
private String[] poemArray = { "去年今日栽", "临去见花开", "好住守空院", "夜间人不来" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定义文字切换器
final TextSwitcher ts = (TextSwitcher) findViewById(R.id.textSwitcher1);
//定义视图显示工厂,并设置
ts.setFactory(new ViewFactory() {
@Override
public View makeView() {
TextView tv =new TextView(TextSwitcherActivity.this);
tv.setTextSize(32);
tv.setTextColor(Color.GREEN);
return tv;
}
});
// 设置文字来源
ts.setText(poemArray[index]);
// 设置点击监听器
ts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
index++;
if(index>=poemArray.length) {
index = 0;
}
ts.setText(poemArray[index]);
}
});
// 设置切入动画
ts.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
ts.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
}
}
源代码:http://115.com/file/dp766658