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但是却发生了滑动