java 在Android中以编程方式仅使用“,0123456789”设置edittext?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11519214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set edittext using only ",0123456789" programmatically in Android?
提问by joonasj
My question is this.
我的问题是这个。
I can say in xml
我可以在 xml 中说
android:digits="0123456789,"
But this time I've to it add trough java code.
但这一次我必须向它添加低谷 java 代码。
customEditText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));
Doesn't work. Application gives me same result as android:InputType="number"
不起作用。应用程序给了我与 android:InputType="number" 相同的结果
So is there any alternative way to say android:digits using java?
那么有没有其他方法可以用 java 说 android:digits 呢?
EDITED
已编辑
So my question wasn't how to get android:InputType="number"
using java.
所以我的问题不是如何android:InputType="number"
使用 java。
My question is that how I can get android:digits="0123456789,"
to be used with java.
我的问题是如何android:digits="0123456789,"
才能使用 java。
At this point I don't care if the user can see characters. I just want my edittext field to accept only numbers from 0 to 9 and the decimal comma (,).
在这一点上,我不在乎用户是否可以看到字符。我只希望我的 edittext 字段只接受 0 到 9 之间的数字和小数点逗号 (,)。
Because of the customEdittext is really a customized edittext I can't use xml atm.
由于 customEdittext 确实是一个自定义的 edittext,我不能使用 xml atm。
回答by Haresh Chaudhary
try customEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
尝试 customEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
回答by cutiko
According to the Android documentation TYPE_NUMBER_FLAG_DECIMAL
should work http://developer.android.com/reference/android/text/InputType.html
根据 Android 文档TYPE_NUMBER_FLAG_DECIMAL
应该可以工作http://developer.android.com/reference/android/text/InputType.html
However it doesn't, if you use only TYPE_NUMBER_FLAG_DECIMAL
the user will be able to input letters as well.
然而它不是,如果你只TYPE_NUMBER_FLAG_DECIMAL
使用用户也可以输入字母。
But by our luck, Haresh Chaudhary, provide the correct answer, both must be use, TYPE_CLASS_NUMBER
and also TYPE_NUMBER_FLAG_DECIMAL
. I try hes solution and It worked.
但幸运的是,Haresh Chaudhary 提供了正确的答案,两者都必须使用,TYPE_CLASS_NUMBER
而且TYPE_NUMBER_FLAG_DECIMAL
. 我尝试了他的解决方案,它奏效了。
Allthough I used setInputType instead of setRawInput:
尽管我使用了 setInputType 而不是 setRawInput:
yourEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);