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
 * 

* Author:XUPF *

* 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





    
    
    

picture_display_name_style






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

你可能感兴趣的:(知识积累)