<转>Android 中文 API (17) —— TextSwitcher

本文详细介绍了Android中TextSwitcher类的使用方法,包括其构造函数、公共方法以及如何通过动画效果实现文本内容的平滑切换。通过代码示例展示了如何在界面上动态展示文本变化,并提供了实现动画文本切换的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

正文

  一、结构

    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

 

  二、类概述

    

    ViewSwitcher仅仅包含子类型TextView。TextSwitcher被用来使屏幕上的label产生动画效果。每当setText(CharSequence)被调用时,TextSwitcher使用动画方式将当前的文字内容消失并显示新的文字内容。(译者注:改变文字时增加一些动画效果)

 

  三、构造函数

         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           需要显示的新文本内容

 

  五、代码示例

    5.1  摘自APIDemos->View->TextSwitcher

      5.1.1  Java

public   class  TextSwitcher1  extends  Activity  implements  ViewSwitcher.ViewFactory,
        View.OnClickListener {

    
private  TextSwitcher mSwitcher;

    
private   int  mCounter  =   0 ;

    @Override
    
protected   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);

        setContentView(R.layout.text_switcher_1);

        mSwitcher 
=  (TextSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(
this );

        Animation in 
=  AnimationUtils.loadAnimation( this ,
                android.R.anim.fade_in);
        Animation out 
=  AnimationUtils.loadAnimation( this ,
                android.R.anim.fade_out);
        mSwitcher.setInAnimation(in);
        mSwitcher.setOutAnimation(out);

        Button nextButton 
=  (Button) findViewById(R.id.next);
        nextButton.setOnClickListener(
this );

        updateCounter();
    }

    
public   void  onClick(View v) {
        mCounter
++ ;
        updateCounter();
    }

    
private   void  updateCounter() {
        mSwitcher.setText(String.valueOf(mCounter));
    }

    
public  View makeView() {
        TextView t 
=   new  TextView( this );
        t.setGravity(Gravity.TOP 
|  Gravity.CENTER_HORIZONTAL);
        t.setTextSize(
36 );
        
return  t;
    }
}

      5.1.2  XML

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值