介绍

介绍

福哥在制作APP的时候,需要限制文本输入框可以输入的内容。经过一顿研究,发现了digits这个属性。使用digits属性可以限制输入框接受的字符类型和范围,范围之内的可以被输入,范围之外的无法输入进来。

设置范围

在strings.xml里设置一个可以输入的字符范围,例如:大写字母,小写字母,数字。abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789

绑定输入框

有了这个范围之后就可以应用到我们的EditText输入框了。

android:id="@+id/txt"

app:layout_constraintTop_toTopOf="parent"

app:layout_constraintLeft_toLeftOf="parent"

android:layout_width="match_parent"

android:layout_height="40dp"

android:layout_marginBottom="30dp"

android:cursorVisible="true"

android:background="#ffffff"

android:textCursorDrawable="@null"

android:singleLine="true"

android:digits="@string/editTextFilter_chrAndInt"/>

总结

使用digits属性配合strings就可以限制用户在输入框里输入的字符了。不过,这个方法有个问题,就是虽然用户不能输入范围外的字符,但是输入法里可以正常“选择”到,这就让用户很郁闷,不知道的还以为是APP故障了。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐