Is there a way to get an Android soft keyboard that has only numbers (no decimals, spaces) with Java code?

社会主义新天地 提交于 2019-12-04 14:23:41

It's not possible using the built-in keyboard. You'll have to develop a custom soft keyboard, or write an inline view that works like a keyboard replacement.

Gautam

I think this question has already been answered on StackOverflow. Look at this:

Only show number buttons on Soft Keyboard in Android?

@twaddington is correct that what you are asking for won't be possible with the built-in keyboard. One thing you can do to prevent non-digits from being entered is set the following in XML for your EditText.

android:inputType="phone"
android:digits="1234567890"

If you want to this in code, and not in XML, I think this should work:

numericField.setInputType(Configuration.KEYBOARD_12KEY);
numericField.setKeyListener(new DigitsKeyListener());

To develop your own numeric soft keyboard, this tutorial may help.

vishesh chandra

Please use inputType = "number", then you will get only number keypad.

<EditText android:inputType="number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

I hope it will help you. :)

There is a way. Use

numericField.setInputType(InputType.TYPE_NUMBER_VARIATION_NORMAL|InputType.TYPE_CLASS_NUMBER);

It anticipates number pad only on Holo, and backward compatible number pad on lower versions.

Seems Android has added the functionality we were seeking. This is the xml I use for simple EditText numeric entry:

    android:inputType="numberPassword"
    android:digits="0123456789"
    android:singleLine="true"
    android:ems="4"
    android:textColor="@android:color/black"
    android:gravity="center"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!