一. 简单示例

src

public class AndroidUIActivity extends Activity {// 索引private int index;// 文本数组private String[] poemArray = { "we", "are", "good", "friends" };/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//定义文字切换器final TextSwitcher ts = (TextSwitcher) findViewById(R.id.textSwitcher1);//定义视图显示工厂,并设置ts.setFactory(new ViewFactory() {public View makeView() {TextView tv =new TextView(AndroidUIActivity.this);tv.setTextSize(32);tv.setTextColor(Color.GREEN);return tv;}});// 设置图片来源ts.setText(poemArray[index]);// 设置点击监听器ts.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// 点击会切换图片index++;if (index >= poemArray.length) {index = 0;}ts.setText(poemArray[index]);}});// 设置切入动画ts.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));// 设置切出动画ts.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));}}

main.xml

<?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" >    <TextSwitcher        android:id="@+id/textSwitcher1"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </TextSwitcher></LinearLayout>

二. 运行结果

启动

Android之TextSwitcher_第1张图片


点击we后(只能点击文字)

Android之TextSwitcher_第2张图片

更多相关文章

  1. Android 图片点击 全屏缩放预览
  2. Android富文本图文混排、粗体、斜体、图片、下划线、删除线等
  3. android调用系统(相机)的图片,并且返回
  4. Android Dialog定义没有标题的注意事项
  5. Android Studio使用AIDL时无法找到自定义的类
  6. Android之十三图片2D翻转
  7. Android 编程下背景图片适配工具类

随机推荐

  1. 可行性验证:API管理与ServiceMesh的集成
  2. 技术解读:金融行业API安全管理规范
  3. 各种容器网络方案性能深度对比
  4. 参数错误 chkdsk无法修复. 不知道怎样寻
  5. jQuery常用dom操作与vue基础
  6. 痞子衡嵌入式:极易上手的可视化wxPython G
  7. 9个REST API设计的基本准则
  8. Vue开发推荐使用的7种模式
  9. Vue开发中可以使用的ES6新特征
  10. 2020年Github星级前20名JavaScript框架性