TextSwitcherProject

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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章