Android textinput where the user can write digits and '/' [duplicate]

一个人想着一个人 提交于 2019-12-24 07:57:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!