要进行切换操作,那么肯定需要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;

}

}

}

更多相关文章

  1. 为什么要学习 Markdown?究竟有什么用?
  2. android 组件生命周期
  3. TextUtils的使用
  4. Android(安卓)UI设计准则
  5. Groovy XML文件/文本处理
  6. Android(安卓)DOC翻译—Processes and Threads
  7. Android架构组件_LiveData
  8. 【Tech-Android-Other】Android中的Future
  9. android in practice_Using Intents(GoodShares project)

随机推荐

  1. Android 利用JNI调用Android Java代码函
  2. Android(安卓)8.0通知不显示
  3. Android中Drawable分类汇总
  4. Android调用系统设置
  5. 关于Android发送邮件
  6. Android(安卓)混淆问题排查
  7. Ubuntu下使用adb和USB连接的方式进行andr
  8. 在android media framework中添加播放器
  9. android2.2中EditText不可编辑的问题
  10. android之TextView属性