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

流过昼夜 提交于 2019-12-21 20:49:46

问题


I have an iPhone app that uses different keyboard layouts. Some are custom and some are built in:

Numbers only:

Decimals:

Custom X button for ISBN numbers:

I'd like to do the same thing on Android, but even the normal InputType.TYPE_CLASS_NUMBER still includes a decimals, space, comma, etc.

How can I customize the keyboards in Android??


回答1:


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.




回答2:


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.




回答3:


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. :)




回答4:


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.




回答5:


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"


来源:https://stackoverflow.com/questions/13771865/is-there-a-way-to-get-an-android-soft-keyboard-that-has-only-numbers-no-decimal

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