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。
1609 0
|
2月前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
3天前
|
Dart 前端开发 Android开发
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
【02】写一个注册页面以及配置打包选项打包安卓apk测试—开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草央千澈
|
1月前
|
搜索推荐 前端开发 API
探索安卓开发中的自定义视图:打造个性化用户界面
在安卓应用开发的广阔天地中,自定义视图是一块神奇的画布,让开发者能够突破标准控件的限制,绘制出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战技巧,逐步揭示如何在安卓平台上创建和运用自定义视图来提升用户体验。无论你是初学者还是有一定经验的开发者,这篇文章都将为你打开新的视野,让你的应用在众多同质化产品中脱颖而出。
65 19
|
2月前
|
IDE Java 开发工具
移动应用与系统:探索Android开发之旅
在这篇文章中,我们将深入探讨Android开发的各个方面,从基础知识到高级技术。我们将通过代码示例和案例分析,帮助读者更好地理解和掌握Android开发。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和技巧。让我们一起开启Android开发的旅程吧!
|
1月前
|
JSON Java API
探索安卓开发:打造你的首个天气应用
在这篇技术指南中,我们将一起潜入安卓开发的海洋,学习如何从零开始构建一个简单的天气应用。通过这个实践项目,你将掌握安卓开发的核心概念、界面设计、网络编程以及数据解析等技能。无论你是初学者还是有一定基础的开发者,这篇文章都将为你提供一个清晰的路线图和实用的代码示例,帮助你在安卓开发的道路上迈出坚实的一步。让我们一起开始这段旅程,打造属于你自己的第一个安卓应用吧!
70 14