您的位置:首页 > 移动开发 > Android开发

android控件17 TextSwitcher

2011-12-18 21:26 363 查看
1)/res/layout/main.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"
>
<TextSwitcher android:id="@+id/textswitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
/>
</LinearLayout>


2)com.sxt.TextSwitcherActivity.java

package com.sxt;

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

public class TextSwitcherActivity extends Activity {
/** Called when the activity is first created. */

int position = 0;
private String [] texts = {"曹操","刘备","孙权","郭嘉","诸葛亮","周瑜"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button)this.findViewById(R.id.button);

//final对象,引用不变。final基本类型,数据不变。
final TextSwitcher textSwitcher = (TextSwitcher)this.findViewById(R.id.textswitcher);

//显示视图,设置工厂类的makeView方法。
textSwitcher.setFactory(new ViewFactory() {

@Override
public View makeView() {
// TODO Auto-generated method stub
TextView textView = new TextView(TextSwitcherActivity.this);
textView.setTextSize(30);
textView.setTextColor(Color.YELLOW);
return textView;
}
});
textSwitcher.setText(texts[position]);
//设置切入动画
textSwitcher.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
//设置切出动画
textSwitcher.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(position < texts.length-1)
{
position ++;
}
else
{
position = 0;
}
textSwitcher.setText(texts[position]);
}
});

}
}


3)如图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: