API Demos 2.3 學習筆記 (19)-- Views-> TextSwitcher
更多精彩內容,請點擊閱讀:《API Demos 2.3 學習筆記》
下麵簡單介紹怎麼創建和使用TextSwitcher控件。
1、首先,在布局文件中定義一個TextSwitcher控件
<TextSwitcher android: android:layout_width="match_parent" android:layout_height="wrap_content" />
2、創建 Activity時,同時繼承ViewSwitcher.ViewFactory接口。
public class TextSwitcher1 extends Activity implements ViewSwitcher.ViewFactory,
3、通過findViewById 實例化TextSwitcher對象
TextSwitcher mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
4、通過如下設置。每次TextSwitcher的setText(CharSequence)方法 ,都會刷新頁麵,並調用 makeView來生成一個TextView對象,用於顯示文字。
mSwitcher.setFactory(this);
//每次調用TextSwitcher的setText方法,UI主線程會調用該函數來生成一個TextView,用於顯示文字。
//這裏可以加上自定義屬性來自定義TextView。例如,我設置了文字的顏色為藍色。
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextColor(Color.BLUE);
t.setTextSize(36);
return t;
}
//加載並設置TextSwitcher的淡入淡出動畫
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);
6、最後,調用setText(CharSequence)方法 刷新界麵。刷新流程參考第4條。
mSwitcher.setText(String.valueOf(mCounter));
下麵我們進行實例代碼解析:
res-layout-text_switcher_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 一個Button對象 -->
<Button
android:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text_switcher_1_next_text" />
<!-- 一個TextSwitcher對象 -->
<TextSwitcher
android:
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
src-com.example.android.apis.view-TextSwitcher1.java
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.graphics.Color;
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;
/**
* 演示如何使用 TextSwitcher 控件
*/
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);
// 通過findViewById獲得一個TextSwitcher對象
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
//加載並設置TextSwitcher的淡入淡出動畫
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);
// 通過findViewById獲得一個Button對象,並設置監聽器
Button nextButton = (Button) findViewById(R.id.next);
nextButton.setOnClickListener(this);
updateCounter();
}
//Button的監聽器實現代碼。
//每次點擊按鈕,mCounter就會加上1,並且刷新界麵
public void onClick(View v) {
mCounter++;
updateCounter();
}
//每次調用TextSwitcher的setText方法,都會刷新界麵。
//用淡出動畫效果隱藏舊的TextView,接著用淡入效果加載新的TextView
private void updateCounter() {
mSwitcher.setText(String.valueOf(mCounter));
}
//每次調用TextSwitcher的setText方法,UI主線程會調用該函數來生成一個TextView,用於顯示文字。
//這裏可以加上自定義屬性來自定義TextView。例如,我設置了文字的顏色為藍色。
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextColor(Color.BLUE);
t.setTextSize(36);
return t;
}
}
預覽效果:

最後更新:2017-04-02 22:16:29