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. (转)个例子让你了解Java反射机制

    个例子让你了解Java反射机制   原文地址:http://blog.csdn.net/ljphhj/article/details/12858767 JAVA反射机制:   通俗地说,反射机制就是可 ...

  2. 用扩展方法实现DevExpress-GridControl级联效果

    首先,让我们先回顾下.Net中扩展方法的特征: 1.  必须在一个非嵌套.非泛型的静态类中: 2.  至少有一个参数(this 作前缀的参数): 3.  第一个参数必须附加this做前缀: 4.  第 ...

  3. HDU 5534 Partial Tree

    2015 ACM/ICPC 长春现场赛 H题 完全背包 #include<cstdio> #include<cstring> #include<cmath> #in ...

  4. 16级第一周寒假作业F题

    Subsequence TimeLimit:1000MS  MemoryLimit:65536K 64-bit integer IO format:%lld Problem Description A ...

  5. dedecms mysql连接错误:#1040 - Too many connections

    mysql能登进去一下,点任何链接又跳出来,然后就登不上了 解决办法:检查mysql所在盘是否还有空间

  6. 2016"百度之星" - 资格赛(Astar Round1) Problem D

    排个序,map直接搞. #include <stdio.h> #include <math.h> #include<cstring> #include<cma ...

  7. Java模拟post提交表单数据

    package test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOExcep ...

  8. 目前所有的ANN神经网络算法大全

    http://blog.sina.com.cn/s/blog_98238f850102w7ik.html 目前所有的ANN神经网络算法大全 (2016-01-20 10:34:17) 转载▼ 标签: ...

  9. Android Studio:Gradle DSL method not found: 'runProguard()'

    Android Studio发布了新的1.0版,更新之后却发现原来在0.8下面正常的项目编译失败了,从报错上来看是卡在gradle上面. Gradle DSL method not found: 'r ...

  10. Quick Cocos2dx controller的初步实现

    很久没有记笔记了,今天记一下,最近都在瞎忙活,都不知道自己干了些啥. 我的Controller是在官方的mvc sample的里面的PlayerDualController上更改的,所以很多地方还没来 ...