How do I default to numeric keyboard on EditText without forcing numeric input? [duplicate]

回眸只為那壹抹淺笑 提交于 2019-11-26 07:38:02

问题


This question already has an answer here:

  • EditText with number keypad by default, but allowing alphabetic characters 19 answers

This has been asked elsewhere online to no avail. Is there any way in Android to display the numeric soft keyboard when focusing on an EditText, but still allow any text to be entered?

I\'d like to let the user enter quantities (e.g. \"1 kg\", \"2 L\"), so just setting inputType=\"number\" won\'t work.


回答1:


Add the following line of code, and it will do the trick :)

editText.setRawInputType(Configuration.KEYBOARD_QWERTY);

This will show the the numeric keypad first, but also allows you to enter free text.

More information here.




回答2:


This may be device dependant but have you tried:

 android:inputType="phone"

All Input Types Link

in the EditText's xml , this gives you the number pad keyboard but then you can still switch to letter's if you want. (Atleast on my Nexus One).




回答3:


Note that: setRawInputType(InputType.TYPE_CLASS_NUMBER);

has the desired effect on some devices but not others...

On htc it works fine however on galaxy tab II you only get the numeric keyboard and no way to switch back to alpha.




回答4:


write the code in XML, android:numeric="integer" android:inputType="phone" android:digits="1234567890"




回答5:


It looks like the underlying question you're dealing with is: how can I allow the user to enter quantities?

One appropriate answer is: with a numeric input, paired with some form of category select for the unit. e.g. radio, dropdown, or spinner. This is probably easier to use and also saves you the headache of having to validate your input every time.

You could also just have iron cojones and write a custom soft keyboard.




回答6:


I tried many different combinations before I figured this out, but this appears to work correctly:

setRawInputType(InputType.TYPE_CLASS_NUMBER);

The key lies in the description for setRawInputType(int):

Directly change the content type integer of the text view, without modifying any other state.



来源:https://stackoverflow.com/questions/6153796/how-do-i-default-to-numeric-keyboard-on-edittext-without-forcing-numeric-input

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