Android 中文 API (17) —— TextSwitcher

简介:

正文

  一、结构

    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

<? 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" >

    
< Button  android:id ="@+id/next"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"  
        android:text
="@string/text_switcher_1_next_text"   />

    
< TextSwitcher  android:id ="@+id/switcher"
        android:layout_width
="match_parent"
        android:layout_height
="wrap_content"   />

</ LinearLayout >


本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/582684,如需转载请自行联系原作者
相关文章
|
Android开发 Java 数据格式
|
Android开发
Android零基础入门第55节:ImageSwitcher和TextSwitcher使用
原文:Android零基础入门第55节:ImageSwitcher和TextSwitcher使用     上一期我们了解了ViewAnimator组件和ViewSwitcher组件的使用,你都掌握了吗?本期一起来学习ViewSwitcher的两个子组件ImageSwitcher和TextSwitcher。
1593 0
|
2天前
|
测试技术 Linux Android开发
探索安卓开发之旅:从初学者到专家
【8月更文挑战第29天】本文是一篇为初学者和有一定经验的开发者准备的安卓开发指南。我们将从基础概念开始,逐步深入到高级主题,如自定义视图、性能优化等。无论你是刚刚入门,还是希望提升自己的技能,这篇文章都将为你提供有价值的信息和建议。让我们一起踏上这段激动人心的旅程吧!
|
1天前
|
供应链 物联网 区块链
未来触手可及:探索新兴技术的趋势与应用安卓开发中的自定义视图:从基础到进阶
【8月更文挑战第30天】随着科技的飞速发展,新兴技术如区块链、物联网和虚拟现实正在重塑我们的世界。本文将深入探讨这些技术的发展趋势和应用场景,带你领略未来的可能性。
|
2天前
|
存储 搜索推荐 Java
探索安卓开发中的自定义视图:打造个性化UI组件Java中的异常处理:从基础到高级
【8月更文挑战第29天】在安卓应用的海洋中,一个独特的用户界面(UI)能让应用脱颖而出。自定义视图是实现这一目标的强大工具。本文将通过一个简单的自定义计数器视图示例,展示如何从零开始创建一个具有独特风格和功能的安卓UI组件,并讨论在此过程中涉及的设计原则、性能优化和兼容性问题。准备好让你的应用与众不同了吗?让我们开始吧!
|
1天前
|
XML 搜索推荐 Android开发
安卓开发中的自定义View组件实践
【8月更文挑战第30天】探索Android世界,自定义View是提升应用界面的关键。本文以简洁的语言带你了解如何创建自定义View,从基础到高级技巧,一步步打造个性化的UI组件。
|
2天前
|
设计模式 Java Android开发
探索安卓应用开发:从新手到专家的旅程探索iOS开发中的SwiftUI框架
【8月更文挑战第29天】本文旨在通过一个易于理解的旅程比喻,带领读者深入探讨安卓应用开发的各个方面。我们将从基础概念入手,逐步过渡到高级技术,最后讨论如何维护和推广你的应用。无论你是编程新手还是有经验的开发者,这篇文章都将为你提供有价值的见解和实用的代码示例。让我们一起开始这段激动人心的旅程吧!