Android 中文 API (17) —— TextSwitcher

來源:互聯網
上載者:User

前言

  本章內容是android.widget.TextSwitcher,譯為文字轉換器控制項(引自系出名門系列),翻譯來自madgoat ,歡迎大家訪問他的部落格:http://madgoat.cn/,再次感謝 madgoat !期待你一起參與Android API 的中文翻譯,聯絡我over140@gmail.com。 

 

聲明

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

    madgoat:http://madgoat.cn/

    部落格園:http://www.cnblogs.com

    農民伯伯: http://www.cnblogs.com/over140/

 

版本

  Android 2.2 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)

         使用提供的context和attributes來建立一個空的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>

    5.2  其他樣本

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

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

 

  六、下載

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

 

結束

   madgoat非常積極,接到本章譯稿後就迅速的翻譯出來,並且附帶完善的代碼和,現在已經在翻譯下一篇譯稿,感謝他如此積极參与!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.