问题
My user has to enter a string like 21/99
where digits and the sign /
are the only valid signs that the user can enter, ideally those would all be shown on the keyboard and little other signs.
Unfortunately, neither number
nor text
not any other type I found produces in such an UI.
Is there a way where I can make it easy for my user to enter such numbers.
回答1:
Use android:digits="1234567890/"
in EditText
android:digits
- If set, specifies that this TextView has a numeric input method and that these specific characters are the ones that it will accept. If this is set
SAMPLE CODE
<EditText
android:layout_width="match_parent"
android:digits="1234567890/"
android:inputType="text"
android:layout_height="wrap_content" />
回答2:
you can try to use Androids/Javas Regular Expressions
If you go with RegEx, you will be able to PREVENT a user to type in everything you dont want him to, e.g. everything that is not a digit (d) or the char "/". So this will solve your problem if you want to do the thinking for your users.
Alternative you can prevent that like answered above with restrictions in XML <EditText/>
if you dont want to do in your Java-Class
来源:https://stackoverflow.com/questions/52203810/android-textinput-where-the-user-can-write-digits-and