要进行切换操作,那么肯定需要ViewFactory,因为如果不设置,那么就无法完成组件的转换,而文本组件转换肯定需要操作TextView。
在main.xml中
<?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" >
<TextSwitcher
android:id="@+id/myTextSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示当前时间"/>
</LinearLayout>
在MyTextSwitcherDemo.java程序中
package com.tarena.textswitcher;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
public class MyTextSwitcherDemo extends Activity {
private TextSwitcher myTextSwitcher = null;
private Button but = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.myTextSwitcher = (TextSwitcher) super
.findViewById(R.id.myTextSwitcher);
this.but = (Button) super.findViewById(R.id.but);
this.myTextSwitcher.setFactory(new ViewFactoryImpl());
this.myTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
this.myTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
this.but.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
public void onClick(View v) {
MyTextSwitcherDemo.this.myTextSwitcher.setText("当前时间为:"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.format(new Date()));
}
}
private class ViewFactoryImpl implements ViewFactory {
public View makeView() {
TextView txt = new TextView(MyTextSwitcherDemo.this);
txt.setBackgroundColor(0xFFFFFFFF);
txt.setTextColor(0xFF000000);
txt.setLayoutParams(new TextSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txt.setTextSize(30);
return txt;
}
}
}