TextSwitcher 简单用例
TextSwitcher 字面理解是文字交换器,是ViewSwitcher的子类,从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,显然是交换TextView。
点击缓慢出现文字
BaseActivity
package com.shaoxin.mytextswitcher; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; /** * Created by shaoxin on 2016/12/4. */ public abstract class BaseActivity extends AppCompatActivity { public abstract void init(); public abstract void listener(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); listener(); } }
MainActivity
package com.shaoxin.mytextswitcher; import android.os.Bundle; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.TextSwitcher; import android.widget.TextView; import android.widget.ViewSwitcher; public class MainActivity extends BaseActivity { private TextSwitcher textswitcher; private Button btn; private ViewSwitcher.ViewFactory factory; @Override public void init() { setContentView(R.layout.activity_main); textswitcher = (TextSwitcher) findViewById(R.id.textswitcher); btn = (Button) findViewById(R.id.btn); } @Override public void listener() { factory = new ViewSwitcher.ViewFactory() { @Override public View makeView() { TextView textView = new TextView(MainActivity.this); textView.setTextSize(40); return textView; } }; textswitcher.setFactory(factory); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textswitcher.setInAnimation(AnimationUtils .loadAnimation(MainActivity.this, android.R.anim.fade_in)); textswitcher.setInAnimation(AnimationUtils .loadAnimation(MainActivity.this, android.R.anim.fade_out)); textswitcher.setText("hello"); } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.shaoxin.mytextswitcher.MainActivity"> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击出现" /> <TextSwitcher android:id="@+id/textswitcher" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>