文本切换器(TextSwitcher)的功能和用法
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)的功能和用法的更多相关文章
- TextSwitcher 文本切换器的功能与用法
TextSwitcher集成了ViewSwitcher, 因此它具有与ViewSwitcher相同的特性:可以在切换View组件时使用动画效果.与ImageSwitcher相似的是,使用TextSwi ...
- 文本切换器(TextSwitcher)的功能与用法
TextSwitcher继承了ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换View组件时使用动画效果.与ImageSwitcher相似的是,使用TextSwit ...
- TextSwitcher(文本切换器)和ViewFlipper
1.TextSwitcher 使用: 应用分为三步: 1.得到 TextSwitcher 实例对象 TextSwitcher switcher = (TextSwitcher) findViewB ...
- Android 自学之网格试图(GridView)和图片切换器(ImageSwitcher)功能和用法
网格试图(GridView)用于在界面上按行,列分布的方式来显示多个组件. GridView和ListView有共同的父类:AbsListView,因此GridView和ListView具有一定的相似 ...
- Android 自学之画廊视图(Gallery)功能和用法
Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...
- C# 方法调用的切换器 Update 2015.02.02
在编写应用程序时,我们经常要处理这样的一组对象,它们的类型都派生自同一个基类,但又需要为每个不同的子类型应用不同的处理方法. 通常的做法,最简单的就是用很多 if-else 去判断各自的类型,如下面的 ...
- KVM切换器
所谓KVM,就是Keyboard.Video.Mouse的缩写,正式的名称为多计算机切换器.简单的说,就是一组键盘.显示器和鼠标,控制2台.4 台.8台.16台甚至到4096台以上的计算机主机. KV ...
- 搜索框(SearchView)的功能与用法
SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索. 使用SearchView时可以使用如下 ...
- vue2 3d 切换器
空闲时写了一个3d切换器,灵感来自于转行前画3d工程图,效果如图: 功能:按住鼠标中间,变为3d模式,点击6个页面中的某一个页面,页面旋转放大,恢复到2d图形,3d图消失.再次点击鼠标中间,恢复为3d ...
随机推荐
- 一些简单的例子让你在Java中能更好的学习并理解循环结构(1)!
一.java中流程控制方式采用三种基本流程结构:顺序结构,选择(分支)结构,循环结构. 1.[if-else 结构] if(1>2){ system.out.println("if条件 ...
- CSS动画 animation与transition
一.区分容易混淆的几个属性和值 先区分一下css中的几个属性:animation(动画).transition(过渡).transform(变形).translate(移动). CSS3中的trans ...
- MySQL:MySQL的安装
一.Linux:MySQL的源码安装 1.安装前的准备 在安装之前需要安装一下必备的包和工具 gcc/g++:MySQL5.6开始,需要使用g++进行编译. cmake:MySQL5.5开始,使用cm ...
- 打印等腰三角形as3
package { import flash.display.Sprite; public class san extends Sprite { public function san() { var ...
- js图片未加载完显示loading效果
<html> <title>js图片未加载完显示loading效果</title> <body> <style> img{float:lef ...
- springMVC 多方法controller
1. 新建web project 2. 加入jar包 3. 写web.xml <?xml version="1.0" encoding="UTF-8"?& ...
- libthread_db
http://timetobleed.com/notes-about-an-odd-esoteric-yet-incredibly-useful-library-libthread_db/
- 约瑟夫环(java面向对象实现)
/* * 作者:zhaop * 功能:约瑟夫环 * */ public class Joseph { public static void main(String[] args) { // TODO ...
- PHP header使用
header()函数的作用是:发送一个原始 HTTP 标头[Http Header]到客户端.标头 (header) 是服务器以 HTTP 协义传 HTML 资料到浏览器前所送出的字串,在标头与 HT ...
- Mybatis3.2.1整合Spring3.1
Mybatis3.2.1整合Spring3.1 根 据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对 Mybatis ...