【安卓学习】文本切换器

TextSwitcher

TextSwitcher 是 ViewSwitcher 的 子 类, 它 与 ImageSwitcher 很 相 似, 都 需 要 设 置 一 个ViewFactory,并且都可以设置切换时所显示的动画效果。不同的是 TextSwitcher 在实现 ViewFactory接口中的 makeView() 方法时,需要返回一个 TextView 组件。

基本语法

xml 复制代码
<TextSwitcher
 android:id="@+id/textSwitcher"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"/>

支持的属性

实例

XML

xml 复制代码
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns app="http://schemas.android.com/apk/res-auto"
	xmlns tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:background="@mipmap/bg"
	tools context="com.mingrisoft.MainActivity">
	<!--定义文本切换器并设置文本切换时的动画效果-->
	<TextSwitcher
		android:id="@+id/textSwitcher"
		android:layout_width="260dp"
		android:layout_height="100dp"
		android:layout_marginTop="50dp"
		android:layout_marginLeft="50dp"
		android:inAnimation="@anim/in_animation"
		android:outAnimation="@anim/out_animation">
	</Textswitcher>
</RelativeLayout>

注意:在XML文件中设置TextSwitcher组件时,需要指定文字切换时进入与退出的动画文件,如代码中的"anim/in_animation"文字切换时的进入动画文件,"anim/out_animation"文字切换时的退出动画文件。这两个动画文件需要手动创建

Activity

java 复制代码
public class MainActivity extends Activity{
	//定义文本切换器
	private TextSwitcher textSwitcher;
	//定义用于计算诗句数组下标
	private int index;
	//定义唐诗数组
	String[] poetry = new String[]{
		"望庐山瀑布",
		"[作者]李白",
		"日照香炉生紫烟,",
		"避看瀑布挂前川。",
		"飞流直下三千尺,",
		"疑是银河落九天。"
	};
	@Override
	protected void onCreate(Bundle savedInstancestate){
		super.onCreate(savedInstancestate);
		setContentview(R.layout.activity_main);
	}
}

在主活动的 onCreate() 方法中,首先获取布局文件中的 TextSwitcher 组件,然后为其设置1 个 ViewFactory,并重写makeView() 方法,最后在重写的 makeView() 方法中创建 1 个用于显示唐诗文字的 TextView。

java 复制代码
	//获取文本切换器组件
	textSwitcher (TextSwitcher)findviewById(R.id.textswitcher);
	//为文本切换器设置Factory
	textSwitcher.setFactory(new ViewSwitcher.ViewFactory(){
		@Override
		public View makeview(){
			//创建文本框组件用于显示唐诗文字
			Textview textview = new Textview(MainActivity.this);
			textView.setTextsize(30);
			//设置文字大小
			textView.setTextColor(Color.BLACK);
			//设置文字颜色为黑色
			return textView;
		}
})

重写onTouchEvent()方法,在该方法中实现单击手机或模拟器屏幕时切换显示的唐诗文字。具体代码如下:

java 复制代码
@Override
public boolean onTouchEvent(MotionEvent event){
	//判断手指单击屏幕时
	if (event.getAction()==MotionEvent.ACTION_DOWN){
	//切换显示的诗句文字
	textSwitcher.setText(poetry[index++%poetry.length ])
	return super.onTouchEvent(event);
}

运行结果

相关推荐
飛_1 小时前
解决VSCode无法加载Json架构问题
java·服务器·前端
木棉软糖4 小时前
一个MySQL的数据表最多能够存多少的数据?
java
程序视点4 小时前
Java BigDecimal详解:小数精确计算、使用方法与常见问题解决方案
java·后端
愿你天黑有灯下雨有伞4 小时前
Spring Boot SSE实战:SseEmitter实现多客户端事件广播与心跳保活
java·spring boot·spring
遇见尚硅谷5 小时前
C语言:*p++与p++有何区别
c语言·开发语言·笔记·学习·算法
Java初学者小白5 小时前
秋招Day20 - 微服务
java
艾莉丝努力练剑5 小时前
【数据结构与算法】数据结构初阶:详解排序(二)——交换排序中的快速排序
c语言·开发语言·数据结构·学习·算法·链表·排序算法
狐小粟同学5 小时前
JavaEE--3.多线程
java·开发语言·java-ee
jz_ddk5 小时前
[HarmonyOS] 鸿蒙LiteOS-A内核深度解析 —— 面向 IoT 与智能终端的“小而强大”内核
物联网·学习·华为·harmonyos
试着5 小时前
零基础学习性能测试第五章:Tomcat的性能分析与调优-Tomcat原理,核心配置项,性能瓶颈分析,调优
学习·零基础·tomcat·性能测试