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代码可以考到如下效果:

文本切换器(TextSwitcher)的功能和用法的更多相关文章

  1. TextSwitcher 文本切换器的功能与用法

    TextSwitcher集成了ViewSwitcher, 因此它具有与ViewSwitcher相同的特性:可以在切换View组件时使用动画效果.与ImageSwitcher相似的是,使用TextSwi ...

  2. 文本切换器(TextSwitcher)的功能与用法

    TextSwitcher继承了ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换View组件时使用动画效果.与ImageSwitcher相似的是,使用TextSwit ...

  3. TextSwitcher(文本切换器)和ViewFlipper

    1.TextSwitcher 使用: 应用分为三步: 1.得到 TextSwitcher 实例对象   TextSwitcher switcher = (TextSwitcher) findViewB ...

  4. Android 自学之网格试图(GridView)和图片切换器(ImageSwitcher)功能和用法

    网格试图(GridView)用于在界面上按行,列分布的方式来显示多个组件. GridView和ListView有共同的父类:AbsListView,因此GridView和ListView具有一定的相似 ...

  5. Android 自学之画廊视图(Gallery)功能和用法

    Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...

  6. C# 方法调用的切换器 Update 2015.02.02

    在编写应用程序时,我们经常要处理这样的一组对象,它们的类型都派生自同一个基类,但又需要为每个不同的子类型应用不同的处理方法. 通常的做法,最简单的就是用很多 if-else 去判断各自的类型,如下面的 ...

  7. KVM切换器

    所谓KVM,就是Keyboard.Video.Mouse的缩写,正式的名称为多计算机切换器.简单的说,就是一组键盘.显示器和鼠标,控制2台.4 台.8台.16台甚至到4096台以上的计算机主机. KV ...

  8. 搜索框(SearchView)的功能与用法

    SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索. 使用SearchView时可以使用如下 ...

  9. vue2 3d 切换器

    空闲时写了一个3d切换器,灵感来自于转行前画3d工程图,效果如图: 功能:按住鼠标中间,变为3d模式,点击6个页面中的某一个页面,页面旋转放大,恢复到2d图形,3d图消失.再次点击鼠标中间,恢复为3d ...

随机推荐

  1. opcode修改

    Smali: if-eqz opcode 38 if-nez opcode 39 SO: CMP R0,#0 00 28 CMP R0,#1 01 28

  2. Hadoop 文本分类 终于跑通了

    Training 入口 package org.wordCount; import java.util.ArrayList; import java.util.List; import org.apa ...

  3. Direct3D中的绘制

    1.顶点缓存和索引缓存 一个顶点缓存是一个包含顶点数据的连续内存空间:一个索引缓存是一个包含索引数据的连续内存空间. 顶点缓存用接口IDirect3DVertexBuffer9表示:索引缓存用接口ID ...

  4. UML类图基本画法

    转自:http://www.cnblogs.com/import-this/p/5330501.html 1. 类 类包括属性方法 下图的生命就是属性 代谢就是方法 2.基本关系 a.泛化(Gener ...

  5. IMCP网际控制协议

    IP协议是TCP/IP协议使用的在网络层传输机制,它是一种不可靠的无连接的数据报协议,但是IP协议假定了底层是不可靠的,因此,要尽最大的努力传输到目的地,但正因为如此,IP协议则没有了保证,也就是说, ...

  6. my97datepicker开始日期小于结束日期格式化时间精确届时分秒

    my97datepicker开始日期小于结束日期格式化时间精确到时分秒 一 , 需求: 结束时间 > 开始时间, 不符合的时间段不能选择.比如我选择开始日期是7月28,那结束的日期将只能从7月2 ...

  7. Fragmen和Activity之间的通信--接口和实现的分离(转)

    Fragmen和Activity之间的通信--接口和实现的分离(转) 分类: Android平台     在平板的开发过程中通常都会采用多个Fragment的实现方式,通常有一个为list的Fragm ...

  8. Android 的Camera架构介绍

    http://java-admin.iteye.com/blog/452464   第一部分 Camera概述Android的Camera包含取景器(viewfinder)和拍摄照片的功能.目前And ...

  9. CodeForces 621A Wet Shark and Odd and Even

    水题 #include<cstdio> #include<cstring> #include<cmath> #include<ctime> #inclu ...

  10. 关于NOMINMAX这个预处理宏

    标准库在<algorithm>头中定义了两个模板函数std::min() 和 std::max().通常用它可以计算某个值对的最小值和最大值.可惜在 Visual C++ 无法使用它们,因 ...