TextSwitcher的使用以及问题记录

TextSwitcher 字面理解是文字交换器,是ViewSwitcher的子类,从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,显然是交换TextView。

1.使用代码

package com.example.myapplication3;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Time:2019/12/27
 * <p>
 * Author:XUPF
 * <p>
 * Description:
 */
public class TextSwitchActivity extends Activity implements ViewSwitcher.ViewFactory {
    private TextSwitcher tsName;
    private EditText etName;
    private Button btName;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_switch);
        final TextSwitcher switcher = (TextSwitcher) findViewById(R.id.tsName);
        tsName = (TextSwitcher)findViewById(R.id.tsName);
        etName = (EditText)findViewById(R.id.etName);
        btName = (Button)findViewById(R.id.btName);
        tsName.setFactory(new NameSwitcherFactory());
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
        switcher.setInAnimation(in);
        switcher.setOutAnimation(out);
        btName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = etName.getText().toString();
                tsName.setText(etName.getText().toString());
            }
        });
    }
    class NameSwitcherFactory implements ViewSwitcher.ViewFactory {
        @Override
        public View makeView() {
            return getLayoutInflater().inflate(R.layout.picture_display_name_style, null);
        }
    }
    @Override
    public View makeView() {
        TextView textView = new TextView(this);
        textView.setTextSize(36);
        return textView;
    }
}

布局代码
activity_text_switch


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextSwitcher
        android:id="@+id/tsName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="50dp"></EditText>
    <Button
        android:id="@+id/btName"
        android:text="确定"
        android:layout_width="match_parent"
        android:layout_height="50dp"></Button>
</LinearLayout>

picture_display_name_style

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ellipsize="middle"
    android:gravity="center"
    android:singleLine="true"
    android:textColor="@color/black"
    android:textSize="18dp"
        >

</TextView>

问题记录:TextSwitcher是需要先设置工厂模式的,项目中出现了一种情况就是因为先滑动的时候保存了一种左右滑动变化的动态效果,但是后来又需要不滑动的动态效果但是工厂模式没有改变 所以就导致了明明只是settext但是却发生了滑动

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值