前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >文字切换(TextSwitcher)使用

文字切换(TextSwitcher)使用

作者头像
李小白是一只喵
发布2020-04-24 08:39:24
2K0
发布2020-04-24 08:39:24
举报
文章被收录于专栏:算法微时光

image.png

目录

TextSwitcher

TextSwitcher 继承自ViewSwitcher, ViewSwitcher继承自ViewAnimator.

使用其实现文字的切换.

使用方式:

代码语言:javascript
复制
    <TextSwitcher
        android:id="@+id/textSwitcher"
        android:layout_width="400dp"
        android:layout_height="400dp">
    </TextSwitcher>

使用实例

activity_main.xml文件:

代码语言:javascript
复制
<?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>

代码:

代码语言:javascript
复制
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;
    /**
     * 文字数组
     */
    final String[] strs = new String[] {
            "数学老师",
            "美食家",
            "天才",
            "儿童"
    };

    /**
     * 当前选中的文字id序号
     */
    private int currentPosition;
    /**
     * 按下点的X坐标
     */
    private float downX;


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

        //TextSwitcher
        mtestSwitcher  = (TextSwitcher) findViewById(R.id.textSwitcher);
        //设置Factory
        mtestSwitcher.setFactory(this);
        //设置OnTouchListener,我们通过Touch事件来切换图片
        mtestSwitcher.setOnTouchListener(this);

        //初始位置为0
        currentPosition = 0;
        //设置动画
        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坐标
                downX = event.getX();
                break;
            }
            case MotionEvent.ACTION_UP:{
                float lastX = event.getX();
                //抬起的时候的X坐标大于按下的时候移动
                if(lastX > downX){
                    if(currentPosition > 0){
                        currentPosition --;
                        mtestSwitcher.setCurrentText(strs[currentPosition]);
                    }else{
                        Toast.makeText(this, "已经是第一", 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;
    }

}

运行结果:

image.png

image.png

参考

Android_TextSwitcher和ImageSwitcher

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • TextSwitcher
  • 使用实例
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档