android EditView自动判断输入长度,并限制字数,然后将光标停在最后

本文介绍如何在Android中使用EditText实现输入字符数的自动判断,并在达到预设字数限制时阻止进一步输入,同时确保光标始终位于文字末尾。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<EditText
        android:id="@+id/myinfoedit_remark_et"
        android:layout_width="fill_parent"
        android:layout_height="120dp"
        android:background="@color/white"
        android:gravity="top"
        android:hint="我的签名"
        android:paddingLeft="15dp"
        android:paddingTop="15dp"
        android:paddingRight="15dp"
        android:textColor="@color/text_222222"
        android:textColorHint="@color/text_c4c4c4"
        android:textSize="14dp" />


<TextView
            android:id="@+id/shufuxianzhi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textColor="#016cd9"
            android:textSize="12dp" />


mRemark_Et.addTextChangedListener(new TextWatcher()
        {//EditView自动判断输入长度
            
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void afterTextChanged(Editable s)
            {
                String content = mRemark_Et.getText().toString();
                int l = content.length();
                mRemarkXianZhi.setText(l + "");//需要将数字转成字符串
                if (l > 30)
                {
                    mRemark_Et.setText(content.substring(0, 30));
                    mRemark_Et.setSelection(30);//EditView设置光标到最后
                }
                
            }
        });


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值