我已阅读 Android:将 EditText 限制为数字 和 如何在 android 的 EditText 上显示数字键盘?.不幸的是,它们似乎都不符合我的需求.
I have read Android: Limiting EditText to numbers and How do I show the number keyboard on an EditText in android?. Unfortunately, none of them seems to fit my needs.
我想将我的 EditText 输入限制为仅限数字.但是,我也希望允许有符号和/或十进制输入.
I want to restrict my EditText input to only numbers. However, I also want to allow signed and/or decimal input.
这是我当前的代码(我需要以编程方式执行此操作):
Here is my current code (I need to do this programmatically):
EditText edit = new EditText(this);
edit.setHorizontallyScrolling(true);
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
有了这个,我的 EditText 愉快地将所有输入限制为数字.不幸的是,它不允许任何其他内容,例如小数点.
With this, my EditText merrily restricts all input to numerical digits. Unfortunately, it doesn't allow anything else, like the decimal point.
如果我将该行更改为 edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
,EditText 将接受所有输入(这不是我想要的......).
If I change that line to edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
, the EditText accepts all input (which isn't what I want...).
我尝试过组合标志(绝望地想看看它是否可行):
I've tried combining flags (in desperation to see if it would work):
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL)
edit.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED)
这也不起作用(EditText 像往常一样接受所有输入).
That didn't work either (the EditText accepted all input as usual).
那么,我该怎么做呢?
尝试使用 TextView.setRawInputType()对应android:inputType
属性.
这篇关于如何将我的 EditText 输入限制为数字(可能是十进制和有符号)输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!