android TextSwitcher

非 Y 不嫁゛ 提交于 2019-11-29 19:52:10

 public class TextSwitcher extends ViewSwitcher

java.lang.Object

android.view.View

      android.view.ViewGroup

           android.widget.FrameLayout

               android.widget.ViewAnimator

                     android.widget.ViewSwitcher

                               android.widget.TextSwitcher

 ImageSwitcher 和TextViewSwitcher都是ViewSwitcher 的子类,ViewSwitcher又是ViewAnimator 的子类,ViewAnimator (FrameLayout的子类)提供了不同View之间切换时的动画效果支持,而ViewAnimator 为FrameLayout的子类,因此ViewAnimator中包含的子View都是叠放在一起,一般情况下支持看到最上面的一个View。

ViewSwitcher只能最多包括两个子View。每次只显示其中一个View,它有两个子类TextSwitcher 和ImageSwitcher 。本例介绍TextSwitcher ,TextSwitcher 种能包含TextView,TextSwitcher主要可以用法文字切换时动画效果。

每当调用setText时,TextSwitcher 使用设定的动画效果显示新文字串和原先文字之间的切换。可以使用addView 为ViewSwitcher 手动添加一个View到ViewSwitcher中,也可以使用ViewSwitcher.ViewFactory 自动创建一个新View。本例使用ViewSwitcher.ViewFactory为TextSwitcher创建一个新View。

构造函数

public TextSwitcher (Context context)

创建一个新的空TextSwitcher

参数

context 应用程序上下文

public TextSwitcher (Context context, AttributeSet attrs)

使用提供的contextattributes来创建一个空的TextSwitcher

参数

context 应用程序环境

attrs 属性集合

公共方法

public void addView (View child, int index, ViewGroup.LayoutParams params)

根据指定的布局参数新增一个子视图

参数

child 新增的子视图

index 新增子视图的位置

params 新增子视图的布局参数

抛出异常

IllegalArgumentException 当子视图不是一个TextView实例时

public void setCurrentText (CharSequence text)

设置当前显示的文本视图的文字内容。非动画方式显示。

参数

text 需要显示的新文本内容

public void setText (CharSequence text)

设置下一视图的文本内容并切换到下一视图。可以动画的退出当前文本内容,显示下一文本内容。

参数

text 需要显示的新文本内容

案例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextSwitcher 
        android:id="@+id/textviewswitcher_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        >
     </TextSwitcher>
     
    <Button 
        android:id="@+id/btn_textviewswitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextSwitcher"
        />
</LinearLayout>

package com.test;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class TextSwitcherDemo extends Activity implements ViewFactory
		 {
	
	private  TextSwitcher  textSwitcher;
	
	private  int counter=0;
	
	private Button   button; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.textswitcher);
		
		textSwitcher=(TextSwitcher)findViewById(R.id.textviewswitcher_id);
		textSwitcher.setFactory(this);
		
		Animation in = AnimationUtils.loadAnimation(this,  android.R.anim.fade_in);  
		Animation out = AnimationUtils.loadAnimation(this,  android.R.anim.fade_out);  
		textSwitcher.setInAnimation(in);  
		textSwitcher.setOutAnimation(out); 
		
		button = (Button)findViewById(R.id.btn_textviewswitcher);
		
		button.setOnClickListener(listener);
		updateCounter();
	}
	
	private OnClickListener  listener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			counter++;
			updateCounter();
		}
	};
	
	private void updateCounter() {
		// TODO Auto-generated method stub
		textSwitcher.setText(String.valueOf(counter));
	}
	
	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		TextView  textView = new TextView(this);
		textView.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL);
		textView.setTextSize(35);
		return textView;
	}

}

3.执行结果:

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!