Android学习笔记 TextSwitcher文本切换组件的使用

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <TextSwitcher 
        android:id="@+id/myTextSwitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button 
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示当前时间" />
LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
    private Button btn=null;
    private TextSwitcher myTextSwitcher=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.btn=(Button)super.findViewById(R.id.btn);
        this.myTextSwitcher=(TextSwitcher)super.findViewById(R.id.myTextSwitcher);
        this.btn.setOnClickListener(new OnClickListenerImpl());
        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));
    }
    
    private class OnClickListenerImpl implements OnClickListener{

        @Override
        public void onClick(View v) {
            MainActivity.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 myTextView=new TextView(MainActivity.this);
            myTextView.setBackgroundColor(0xFFFFFFFF);
            myTextView.setTextColor(0xFF000000);
            myTextView.setLayoutParams(new TextSwitcher.LayoutParams(
                LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT
            ));
            return myTextView;
        }
        
    }

 

转载于:https://www.cnblogs.com/taobox/p/3356766.html

你可能感兴趣的:(Android学习笔记 TextSwitcher文本切换组件的使用)