• 文本切换器(TextSwitcher)的功能和用法


          TextSwitcher继承了ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换View组件的同时使用动画效果。与ImageSwitcher相似的是,使用TextSwitcher也需要设置一个ViewFactory。与ImageSwitcher不同的是,TextSwitcher所需的ViewFactory的makeView()方法必须返回一个TextView组件。

         实例:文本切换器TextSwitccher

         界面布局文件如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
      >
      <!-- 定义一个TextSwitcher,并指定了文本切换时的动画效果 -->
       <TextSwitcher android:id="@+id/textSwitcher"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:inAnimation="@android:anim/slide_in_left"
           android:outAnimation="@android:anim/slide_out_right"
           android:onClick="next"/>
    </LinearLayout>

    上面的布局文件中定义了一个TextSwitcher,并为该文本切换器指定了文本切换时的动画效果。接下来Activity只要为该TextSwitcher设置ViewFactory,该TextSwitcher即可正常工作。

        下面是该Activity的代码:

    package org.crazyit.helloworld;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.Menu;
    import android.view.View;
    import android.widget.TextSwitcher;
    import android.widget.TextView;
    import android.widget.ViewSwitcher;
    
    public class TextSwitcherTest extends Activity {
        TextSwitcher textSwitcher; 
        
        String[] strs=new String[]{
                "疯狂Java讲义",
                "轻量级Java EE企业应用实战",
                "疯狂Android讲义",
                "疯狂Ajax讲义"
        };
        int curStr;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.text_switcher_test);
            textSwitcher=(TextSwitcher)findViewById(R.id.textSwitcher);
            textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
                
                @Override
                public View makeView() {
                    // TODO Auto-generated method stub
                    TextView tv=new TextView(TextSwitcherTest.this);
                    tv.setTextSize(40);
                    tv.setTextColor(Color.MAGENTA);
                    return tv;
                }
            });
            next(null);
        }
         
        //事件处理函数,控制显示下一个字符串
        public void next(View source)
        {
            textSwitcher.setText(strs[curStr++%strs.length]);
            
        }
        
        
        
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.text_switcher_test, menu);
            return true;
        }
    
    }

         上面的代码重写了ViewFactory的makeView()方法,该方法返回一个TextView,这样即可让TextSwitcher正常工作。当程序要切换TextSwitcher显示文本时,调用TextSwitcher的setText()方法修改文本即可。

         运行上面的Activity代码可以考到如下效果:

  • 相关阅读:
    [Javascript]发布一个自己写的日期控件:DateTimeList
    Oracle PL/SQL 编程手册(SQL大全)
    [乱七八糟][转]程序版吉祥三宝
    [乱七八糟][转]这不是你想象中的软件产业
    [随文杂记]生男好还是生女好?
    [SqlServer]链接数据库存储过程
    [音乐天堂]辛德勒名单原声大碟
    [C#]WinFrom中的DataGrid单击选择行
    [乱七八糟]《进化论——人类科学史上最大的谎言》
    [乱七八糟]《阿甘正传》点评
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/3388517.html
Copyright © 2020-2023  润新知