Why Android TextView addTextChangedListener causes setEllipsize and setMaxLines to be invalid?

Viewed 49

When I use the "addTextChangedListener" api to the Android TextView, the original effect of "setEllipsize" and "setMaxLines" in multi-line will be invalid.

Why? And does anyone know how to solve it? I want them to be compatible with each other

I have tried various ways to call them, but it still doesn’t work.

This is my simple activity code:

public class TestActivity extends Activity {

private static final String TAG = "TestActivity";
TextView tv;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    tv = findViewById(R.id.tv_test);
    tv.setEllipsize(TextUtils.TruncateAt.END);
    tv.setMaxLines(3);
    tv.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.i(TAG,"beforeTextChanged s:" + s.toString());
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Log.i(TAG,"onTextChanged s:" + s.toString());
        }
        @Override
        public void afterTextChanged(Editable s) {
            Log.i(TAG,"afterTextChanged s:"+s.toString());
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    String text = "The annual Apple new product launch conference is here again. Although the basic information was revealed before the launch event as in previous years";
    tv.setText(text, TextView.BufferType.SPANNABLE);
}

}

This is my simple layout xml code:

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

    <TextView
        android:id="@+id/tv_test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="3"
        android:singleLine="false"
        android:textSize="25sp"
        />

</LinearLayout>

Here is my build.gradle(app) code:

buildscript {
    ext {
        kotlin_version = '1.4.21'
    }
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.android.emojicompat"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    // Support Libraries
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.emoji:emoji:1.0.0'
    implementation 'androidx.emoji:emoji-appcompat:1.0.0'
    implementation 'androidx.emoji:emoji-bundled:1.0.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    // Test
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    androidTestImplementation "androidx.test.espresso:espresso-intents:3.1.1"

    testImplementation 'junit:junit:4.12'
}

The following is the effect picture currently affected Here is the Img link

In my imagination, it should appear with an ellipsis after the last word in the third line,but now there is nothing happen.

Thanks to everyone who lends a helping hand

0 Answers
Related