android 之 TextSwitcher 学习

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ethan.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".TextSwitcherActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextSwitcher
        android:id="@+id/textSwitcher1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TextSwitcher>

</LinearLayout>

package com.ethan.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class TextSwitcherActivity extends Activity {
	  // 索引
	private int index;
	
	 // 文本数组
	private String[] poemArray = { "去年今日栽", "临去见花开", "好住守空院", "夜间人不来" };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //定义文字切换器
        final TextSwitcher ts = (TextSwitcher) findViewById(R.id.textSwitcher1);

      //定义视图显示工厂,并设置
        ts.setFactory(new ViewFactory() {
       
        	            @Override
        	            public View makeView() {
    
        	                TextView tv =new TextView(TextSwitcherActivity.this);
        	                tv.setTextSize(32);
        	                tv.setTextColor(Color.GREEN);
        	                return tv;
        	            }
         });
        
     // 设置文字来源
       ts.setText(poemArray[index]);
    // 设置点击监听器
       ts.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			index++;
			if(index>=poemArray.length) {
				index = 0;
			}
			ts.setText(poemArray[index]);
		}
	});
       
    // 设置切入动画
       ts.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
       
       ts.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
    }
}

源代码:http://115.com/file/dp766658


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值