Android初級之組件基礎10-TextSwitcher

來源:互聯網
上載者:User

TextSwitcher,用來使螢幕上的label產生動畫效果,當setText()方法被調用時,TextSwitcher以動畫的形式將當前的文字內容消失並顯示新的文字內容。

1.常用方法
setCurrentText(CharSequence text)
設定當前顯示的文本視圖的文字內容(非動畫方式顯示)

setText(CharSequence text)
設定下一視圖的常值內容,並切換到下一視圖。可以動畫地退出當前常值內容,顯示下一常值內容。

2.執行個體
布局檔案XML如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent">  <Button android:id="@id/btnBaseTextSwitcher" android:layout_width="wrap_content" android:layout_height="wrap_content"android:text="點擊變幻文本" android:hint="以動畫形式變換文本"/><TextSwitcher android:id="@id/tsBase"android:layout_width="wrap_content" android:layout_height="wrap_content"/></LinearLayout>

代碼如下:

import java.util.Random;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.TextSwitcher;import android.widget.TextView;import android.widget.ViewSwitcher;import cn.youtous.R;public class BaseTextSwitcher extends Activity {private Button btnBase;private TextSwitcher tsBase;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.setContentView(R.layout.base_textswitcher);btnBase = (Button)findViewById(R.id.btnBaseTextSwitcher);btnBase.setOnClickListener(new BaseTextSwitcherClickListener());tsBase = (TextSwitcher)findViewById(R.id.tsBase);tsBase.setFactory(new BaseTextSwitcherFactory());setupAnimation();}/** * 設定動畫形式 */private void setupAnimation(){Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);in.setDuration(2000);//為了突齣動畫效果,我們設定動畫事件為2秒鐘Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);out.setDuration(2000);tsBase.setInAnimation(in);tsBase.setOutAnimation(out);}/** * ViewFactory */class BaseTextSwitcherFactory implements ViewSwitcher.ViewFactory {@Overridepublic View makeView() {TextView t = new TextView(BaseTextSwitcher.this);        t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);        t.setTextSize(36);        return t;}}/** * 按鈕的點擊事件監聽器 */class BaseTextSwitcherClickListener implements View.OnClickListener {@Overridepublic void onClick(View v) {Random rand = new Random();tsBase.setText(String.valueOf(rand.nextInt()));}}}

我們可以注意到,如果僅為TextSwitcher設定ViewFactory,不設定動畫時,TextSwitcher只是簡單地對文字進行更換。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.