文字切換(TextSwitcher)使用

image.png

目錄

TextSwitcher

TextSwitcher 繼承自ViewSwitcher, ViewSwitcher繼承自ViewAnimator.

使用其實(shí)現(xiàn)文字的切換.

使用方式:

    <TextSwitcher
        android:id="@+id/textSwitcher"
        android:layout_width="400dp"
        android:layout_height="400dp">
    </TextSwitcher>

使用實(shí)例

activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextSwitcher
        android:id="@+id/textSwitcher"
        android:layout_width="400dp"
        android:layout_height="400dp">
    </TextSwitcher>

</android.support.constraint.ConstraintLayout>

代碼:

package com.example.user.viewsw;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

public class MainActivity extends Activity implements ViewSwitcher.ViewFactory, View.OnTouchListener {
    /**
     * TextSwitcher 的引用
     */
    private TextSwitcher mtestSwitcher;
    /**
     * 文字?jǐn)?shù)組
     */
    final String[] strs = new String[] {
            "數(shù)學(xué)老師",
            "美食家",
            "天才",
            "兒童"
    };

    /**
     * 當(dāng)前選中的文字id序號
     */
    private int currentPosition;
    /**
     * 按下點(diǎn)的X坐標(biāo)
     */
    private float downX;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //TextSwitcher
        mtestSwitcher  = (TextSwitcher) findViewById(R.id.textSwitcher);
        //設(shè)置Factory
        mtestSwitcher.setFactory(this);
        //設(shè)置OnTouchListener,我們通過Touch事件來切換圖片
        mtestSwitcher.setOnTouchListener(this);

        //初始位置為0
        currentPosition = 0;
        //設(shè)置動畫
        mtestSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mtestSwitcher.setOutAnimation( AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));

    }

    @Override
    public View makeView() {
        final TextView textView = new TextView(this);
        textView.setTextSize(100);
        textView.setLayoutParams(new TextSwitcher.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
        textView.setTextColor(Color.rgb(255, 0, 0));
        textView.setText(strs[currentPosition]);
        return textView;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:{
                //手指按下的X坐標(biāo)
                downX = event.getX();
                break;
            }
            case MotionEvent.ACTION_UP:{
                float lastX = event.getX();
                //抬起的時(shí)候的X坐標(biāo)大于按下的時(shí)候移動
                if(lastX > downX){
                    if(currentPosition > 0){
                        currentPosition --;
                        mtestSwitcher.setCurrentText(strs[currentPosition]);
                    }else{
                        Toast.makeText(this, "已經(jīng)是第一", Toast.LENGTH_SHORT).show();
                    }
                }

                if(lastX < downX){
                    if(currentPosition < strs.length - 1){
                        currentPosition ++ ;
                        mtestSwitcher.setCurrentText(strs[currentPosition]);

                    }else{
                        Toast.makeText(this, "到了最后", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            break;
        }

        return true;
    }

}

運(yùn)行結(jié)果:


image.png

image.png

參考

Android_TextSwitcher和ImageSwitcher

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,322評論 0 17
  • 1.下列哪些語句關(guān)于內(nèi)存回收的說明是正確的? (b )A、 程序員必須創(chuàng)建一個(gè)線程來釋放內(nèi)存B、內(nèi)存回收程序負(fù)責(zé)釋...
    悠悠我心_4a00閱讀 607評論 0 2
  • 1.什么是Activity?問的不太多,說點(diǎn)有深度的 四大組件之一,一般的,一個(gè)用戶交互界面對應(yīng)一個(gè)activit...
    JoonyLee閱讀 5,856評論 2 51
  • 0.Android手機(jī)操作系統(tǒng)的四層架構(gòu)? Applications , Application Framewor...
    lucas777閱讀 8,138評論 0 16
  • 開發(fā)者賬號分類 個(gè)人賬號($99)==688功能:可以真機(jī)調(diào)試, 打包測試, 程序發(fā)布. ::優(yōu)點(diǎn):申請速度快, ...
    flowerflower閱讀 395評論 0 1

友情鏈接更多精彩內(nèi)容