Android studio edit text 实现实时记录当前输入字数

之前看过一个博主写的算是逆向的,他是最开始是满的,然后随着输入字数的增加,显示当前剩余字数;我在他的基础上做了改编,因为我想要的是最开始计数为0,随着输入字数增加显示当前已经输入的字符数。
https://blog.csdn.net/a873228446/article/details/43451091 这是他的文章

下面是我对他的代码做的更改(这些是写在OnCreate里的):

editText=findViewById(R.id.editText); 
maxview=findViewById(R.id.max_num);

maxview.setText(0+"/140");  //140是我限制的最大输入字符数
editText.addTextChangedListener(new TextWatcher() {

    int current_Length=0;

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(current_Length <140){
            current_Length = editText.getText().length();
        }
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        maxview.setText(current_Length+"/140");
    }
    @Override
    public void afterTextChanged(Editable s) {
        maxview.setText(current_Length+"/140");
    }
});

一个很简单的布局文件:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <TextView
        android:layout_width="109dp"
        android:layout_height="32dp"
        android:lineSpacingExtra="27dp"
        android:text="说点啥"
        android:textColor="#333333"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.138"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.155" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="309dp"
        android:layout_height="162dp"
        android:background="@android:drawable/edit_text"
        android:hint="随便说呗 还能咋地"
        android:inputType="text|textMultiLine"
        android:maxLength="140"
        android:maxLines="6"
        android:textColorHint="#999999"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.413"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.34" />

    <TextView
        android:id="@+id/max_num"
        android:layout_width="66dp"
        android:layout_height="36dp"
        android:lineSpacingExtra="27dp"
        android:textColor="#ff333333"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.921"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />


    <Button
        android:layout_width="295dp"
        android:layout_height="39dp"
        android:background="@drawable/button1"
        android:lineSpacingExtra="17dp"
        android:onClick="onclick1"
        android:text="提交"
        android:textColor="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.505"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.669" />


</android.support.constraint.ConstraintLayout>

运行效果:

初始界面

输入字符后

猜你喜欢

转载自blog.csdn.net/qq_38110571/article/details/81202870