安卓学习第26课——textSwitcher

点击文字,实现文字转换,只用到了数组,还有动画效果,事件监听。

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

    <TextSwitcher
        android:id="@+id/textSwitcher1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:inAnimation="@android:anim/slide_in_left"
        android:outAnimation="@android:anim/slide_out_right" 
        android:onClick="next"/>
</LinearLayout>
package com.example.textswitcher;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {

    TextSwitcher textSwitcher;
    String[] strs=new String[]{
            "高等数学",
            "线性代数",
            "离散数学",
            "安卓讲义"
    };
    int curStr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textSwitcher=(TextSwitcher) findViewById(R.id.textSwitcher1);
        textSwitcher.setFactory(new ViewFactory(){

            @Override
            public View makeView() {
                TextView tv=new TextView(MainActivity.this);
                tv.setTextSize(40);
                tv.setTextColor(Color.MAGENTA);
                return tv;
            }
            
        });
        next(null);
    }
    public void next(View v) {
        textSwitcher.setText(strs[curStr++%strs.length]);
        
    }

    

}
原文地址:https://www.cnblogs.com/Yvettey-me/p/3967112.html