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

Android输入字数限制
本文介绍了一个简单的Android应用程序,该程序使用EditText控件限制用户输入的字符数量,并实时显示已输入字符数。通过TextWatcher监听输入变化,更新显示。
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>

运行效果:

初始界面

输入字符后

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值