Android 中文 API (17) —— TextSwitcher

前言html

  本章內容是android.widget.TextSwitcher,譯爲文字轉換器控件(引自系出名門系列),翻譯來自madgoat ,歡迎你們訪問他的博客:http://madgoat.cn/,再次感謝 madgoat !期待你一塊兒參與Android API 的中文翻譯,聯繫我over140@gmail.com。 java

 

 

聲明android

  歡迎轉載,但請保留文章原始出處:)
 ide

    madgoat:http://madgoat.cn/函數

    農民伯伯:http://over140.blog.51cto.com/佈局

     

版本動畫

  Android 2.2 r1  this

 

 

正文spa

  1、結構.net

    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

 

 

  2、類概述

 

 

ViewSwitcher僅僅包含子類型TextView。TextSwitcher被用來使屏幕上的label產生動畫效果。每當setText(CharSequence)被調用時,TextSwitcher使用動畫方式將當前的文字內容消失並顯示新的文字內容。(譯者注:改變文字時增長一些動畫效果)

 

  3、構造函數
 

         public TextSwitcher (Context context)

         建立一個新的空TextSwitcher

                   參數

context 應用程序上下文

 

         public TextSwitcher (Context context, AttributeSet attrs)

         使用提供的contextattributes來建立一個空的TextSwitcher

                   參數

                            context 應用程序環境

                            attrs                   屬性集合

 

 

  4、公共方法

 

 

         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、代碼示例

    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 >

    5.2  其餘示例

      http://tech.ddvip.com/2010-02/1265125017144500.html

      http://www.javaeye.com/topic/569985

 

  6、下載

    CSDN:http://download.csdn.net/source/2774515

 

結束
 

   madgoat很是積極,接到本章譯稿後就迅速的翻譯出來,而且附帶完善的代碼和截圖,如今已經在翻譯下一篇譯稿,感謝他如此積極參與!  

相關文章
相關標籤/搜索