控件:文本切换 TextSwitcher

◆TextSwitcher和ViewFactory的使用

View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id
="@+id/MyLayout"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent">
<TextSwitcher
android:id="@+id/myTextSwitcher"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"/>
<Button
android:id="@+id/but"
android:text
="显示当前时间"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"/>
</LinearLayout>
View Code
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.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class MyTextSwitcherDemo extends Activity {
// 文字切换
private TextSwitcher txtsw = null;
// 按钮组件
private Button but = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 调用布局管理器
super.setContentView(R.layout.main);
this.txtsw = (TextSwitcher) super.findViewById(R.id.myTextSwitcher) ;
// 取得组件
this.but = (Button) super.findViewById(R.id.but) ;
// 设置转换工厂
this.txtsw.setFactory(new ViewFactoryImpl());
// 设置动画
this.txtsw.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
// 设置动画
this.txtsw.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
// 定义监听
this.but.setOnClickListener(new OnClickListenerImpl()) ;
}

private class OnClickListenerImpl implements OnClickListener {
@Override
public void onClick(View v) {
MyTextSwitcherDemo.this.txtsw.setText("当前时间为:"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
.format(new Date())); // 显示当前时间

}
}
private class ViewFactoryImpl implements ViewFactory {
@Override
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;
}

}
}
View Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
="org.lxh.demo"
android:versionCode
="1"
android:versionName
="1.0">
<uses-sdk android:minSdkVersion="3" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyTextSwitcherDemo"
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>

                 

 TextSwitcher与ImageSwitcher一样,

如果需要切换都必须通过ViewFactory接口设置。

原文地址:https://www.cnblogs.com/androidsj/p/2379188.html