TextSwitcher继承了ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换View组件的同时使用动画效果。与ImageSwitcher相似的是,使用TextSwitcher也需要设置一个ViewFactory。与ImageSwitcher不同的是,TextSwitcher所需的ViewFactory的makeView()方法必须返回一个TextView组件。

实例:文本切换器TextSwitccher

     界面布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- 定义一个TextSwitcher,并指定了文本切换时的动画效果 -->
<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>

上面的布局文件中定义了一个TextSwitcher,并为该文本切换器指定了文本切换时的动画效果。接下来Activity只要为该TextSwitcher设置ViewFactory,该TextSwitcher即可正常工作。

下面是该Activity的代码:

package org.crazyit.helloworld;

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; public class TextSwitcherTest extends Activity {
TextSwitcher textSwitcher; String[] strs=new String[]{
"疯狂Java讲义",
"轻量级Java EE企业应用实战",
"疯狂Android讲义",
"疯狂Ajax讲义"
};
int curStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_switcher_test);
textSwitcher=(TextSwitcher)findViewById(R.id.textSwitcher);
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override
public View makeView() {
// TODO Auto-generated method stub
TextView tv=new TextView(TextSwitcherTest.this);
tv.setTextSize(40);
tv.setTextColor(Color.MAGENTA);
return
tv;
}

});
next(null);
} //事件处理函数,控制显示下一个字符串
public void next(View source)
{
textSwitcher.setText(strs[curStr++%strs.length]); } @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.text_switcher_test, menu);
return true;
} }

上面的代码重写了ViewFactory的makeView()方法,该方法返回一个TextView,这样即可让TextSwitcher正常工作。当程序要切换TextSwitcher显示文本时,调用TextSwitcher的setText()方法修改文本即可。

运行上面的Activity代码可以考到如下效果:

最新文章

  1. idea打包jar的多种方式
  2. Thinking in Java——笔记(19)
  3. JS 生成GUID 方法
  4. TabControl 显示彩色的图示 (XE6 Firemonkey)
  5. iOS UILocalNotification 每2周,每两个月提醒
  6. Chart控件,把Y轴设置成百分比
  7. Demo3使用bootstrap
  8. Ubuntu下VSFTPD(六)(常见FTP命令及其功能) (
  9. 代码之美——Doom3源代码赏析1
  10. java面向对象值类属语句块
  11. 委托与Lambda-浅谈
  12. Oracle fga审计有这几个特性
  13. GotoTest
  14. WeakSelf宏的进化(转载)
  15. 递归求和1到n
  16. 微信小程序大全(上)(最新整理 建议收藏)
  17. El表达式取map值
  18. Idea在@Autowired注入时报错
  19. centos7.2 安装 mysql5.7
  20. python技巧 计算字符串中字母出现的次数并取出最大

热门文章

  1. 微信公众号系列 --- ionic在IOS的键盘弹出问题
  2. 转:Selenium的延迟等待
  3. PAT (Advanced Level) 1061. Dating (20)
  4. 个人学习FPGA的初步过程
  5. JavaScript运行原理解析
  6. Moocryption
  7. Socket.io官方聊天室DEMO的学习笔记
  8. PAT (Advanced Level) 1014. Waiting in Line (30)
  9. [Big Data]hadoop分布式系统在Linux系统安装
  10. curl调用openstack API总结