Android开发之文本切换组件TextSwi…

Android开发之文本切换组件TextSwitcher

.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextSwitcher 
        android:id="@+id/textSwitch"
        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>

.java

package com.example.textswitcherdemo;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
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 MainActivity extends Activity {
private TextSwitcher myTextSwitcher=null;
private Button button=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myTextSwitcher=(TextSwitcher) super.findViewById(R.id.textSwitch);
this.button=(Button) super.findViewById(R.id.but);
this.button.setOnClickListener(new OnClickListenerImp());
this.myTextSwitcher.setFactory(new ViewFactoryImp());
this.myTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(
this, 
android.R.anim.fade_in
));
this.myTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(
this,
android.R.anim.fade_out
));
}
private class OnClickListenerImp implements OnClickListener{

public void onClick(View v) {
MainActivity.this.myTextSwitcher.setText(
"当前时间为:"+new SimpleDateFormat("yyyy-MM-dd HH:ss:SS").format(new Date())
);
}
}
private class ViewFactoryImp implements ViewFactory{

public View makeView() {
TextView txt=new TextView(MainActivity.this);
txt.setBackgroundColor(0xFFFFFF);
txt.setTextColor(0XFF000000);
txt.setLayoutParams(new TextSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT
));
txt.setTextSize(30);
return txt;
}
}
}

你可能感兴趣的:(Android开发之文本切换组件TextSwi…)