您的位置:首页 > 其它

TextSwitcherProject

2014-07-17 23:50 232 查看

MyDemo.java

package com.jackie.textswitcherproject;

import java.text.SimpleDateFormat;

import java.util.Date;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

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 MyDemo extends Activity {
private TextSwitcher myTextSwitcher = null;
private Button but = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.myTextSwitcher = (TextSwitcher) super
.findViewById(R.id.myTextSwitcher);
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 = (Button) super.findViewById(R.id.but);
this.but.setOnClickListener(new OnClickListenerImpl());
}

private class OnClickListenerImpl implements OnClickListener {

@Override
public void onClick(View arg0) {
MyDemo.this.myTextSwitcher.setText("显示的时间为:"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss")
.format(new Date()));

}
}

private class ViewFactoryImpl implements ViewFactory {

@Override
public View makeView() {
TextView text = new TextView(MyDemo.this);
text.setBackgroundColor(0xffffffff);
text.setTextColor(0xff000000);
text.setLayoutParams(new TextSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
text.setTextSize(30);
return text;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my_demo, menu);
return true;
}

}

main.xml

<RelativeLayout 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"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MyDemo" >

    <TextSwitcher

        android:id="@+id/myTextSwitcher"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

         />

     <Button 

         android:id="@+id/but"

         android:layout_below="@id/myTextSwitcher"

         android:layout_width="wrap_content"

         android:layout_height="wrap_content"

         android:text="切换时间"/>

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