EditText 中带有 numberDecimal inputType 的小数分隔符逗号 (',')
EditText
中的 inputType
numberDecimal
使用点 .
作为小数点分隔符。在欧洲,通常使用逗号 ,
代替。即使我的语言环境设置为德语,小数点分隔符仍然是 .
有没有办法将逗号作为小数点分隔符?
The inputType
numberDecimal
in EditText
uses the dot .
as decimal separator. In Europe it's common to use a comma ,
instead. Even though my locale is set as german the decimal separator is still the .
Is there a way to get the comma as decimal separator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
此处提供的“数字”解决方案的变体:
考虑区域设置分隔符。
A variation on the 'digit' solutions offered here:
Taking into account the locale separator.
以下代码用于 EditText ($ 123,125.155)
Xml 布局
代码的货币掩码
Following Code Currency Mask for EditText ($ 123,125.155)
Xml Layout
Code
这是 Android SDK 中的已知错误。
唯一的解决方法是创建您自己的软键盘。您可以找到实现示例
This is a known bug in the Android SDK.
The only workaround is to create your own soft keyboard. You can find an example of implementation here.
您可以将以下内容用于不同的区域设置
you could use the following for different locales
您可以使用以下解决方法将逗号作为有效输入:-
通过 XML:
以编程方式:
这样 Android 系统将显示数字键盘并允许输入的逗号。希望这能回答这个问题:)
You can use the following workaround to also include comma as a valid input:-
Through XML:
Programmatically:
In this way Android system will show the numbers' keyboard and allow the input of comma. Hope this answers the question :)
如果您以编程方式实例化 EditText,马丁的答案将不起作用。我继续修改了 API 14 中包含的
DigitsKeyListener
类,以允许逗号和句点作为小数分隔符。要使用此功能,请在
EditText
上调用setKeyListener()
,例如,但是,您仍然需要在
TextChangedListener
中使用 Martin 的技巧,其中替换逗号有句点Martins answer won't work if you are instantiating the EditText programmatically. I went ahead and modified the included
DigitsKeyListener
class from API 14 to allow for both comma and period as decimal separator.To use this, call
setKeyListener()
on theEditText
, e.g.However, you still have to use Martin's trick in the
TextChangedListener
where you replace commas with periods恕我直言,解决这个问题的最佳方法是仅使用输入过滤器。这里有一个很好的要点 DecimalDigitsInputFilter。然后你就可以:
IMHO the best approach for this problem is to just use the InputFilter. A nice gist is here DecimalDigitsInputFilter. Then you can just:
我有一个解决方案,允许用户输入点和逗号(如果键盘上可用),但只显示区域设置默认分隔符。此外,它不允许用户输入超过 1 个分隔符。对
EditText
的引用或无限循环没有问题。它是该线程中几个答案的组合,适合我的需求。与接受的答案一样,相应地配置
EditText
:然后在 EditText 上设置自定义 TextWatcher:
并包含自定义 TextWatcher:
}
I have a solution that allows the user to enter both the dot and the comma (if available on the keyboard), but only displays the locale default separator. In addition it will not allow the user to enter more than 1 separator. No issues with references to
EditText
or infinite loops. It is a combination of several answers in this thread suited to my needs.As with the accepted answer, configure the
EditText
accordingly:Then set a custom TextWatcher on the EditText:
And include the custom TextWatcher:
}
对于 Mono(Droid) 解决方案:
For Mono(Droid) solutions:
这里的所有其他帖子都有重大漏洞,所以这里有一个解决方案:
在XML中:
类变量:
在onCreate中,找到当前语言环境中使用的分隔符:
还有onCreate,用这个来更新如果您加载当前值,则为:
此外,在创建时添加侦听器
editTextWatcher
doSomething() 示例,转换为数据的标准周期操纵
All the other posts here had major holes in them, so here's a solution that will:
In the XML:
Class variable:
In onCreate, find the separator used in the current locale:
Also onCreate, Use this to update it if you're loading in a current value:
Also onCreate, Add the Listeners
editTextWatcher
doSomething() example, convert to standard period for data manipulation
您可以执行以下操作:
然后您可以使用输入过滤器:
}
You could do the following:
And then you could use an input filter:
}
要本地化您的输入,请使用:
然后添加:
不要忘记将“,”替换为“。”所以 Float 或 Double 可以毫无错误地解析它。
to get localize your input use:
and then add:
than don't forget to replace "," with "." so Float or Double can parse it without errors.
我可以确认建议的修复程序不适用于三星 IME(至少在 S6 和 S9 上),也许还有 LG。无论区域设置如何,它们仍然显示一个点作为小数分隔符。切换到 Google 的 IME 可以解决这个问题,但对于大多数开发人员来说这并不是一个选择。
Oreo 中也没有修复这些键盘的问题,因为这是三星和/或 LG 必须做的修复,然后甚至推送到他们古老的手机上。
相反,我分叉了 number-keyboard 项目 并添加了一种其行为类似于 IME 的模式: 分叉。有关详细信息,请参阅项目示例。这对我来说效果很好,类似于您在银行应用程序中看到的许多“PIN 输入”假 IME。
I can confirm that the fixes proposed do not work on Samsung IMEs (at least on S6 and S9) and maybe LG. They still show a dot as decimal separator regardless of locale. Switching to Google's IME fixes this but is hardly an option for most developers.
It also has not been fixed in Oreo for these keyboards since it is a fix that Samsung and/or LG have to do and then to push even to their ancient handsets.
I have instead forked the number-keyboard project and added a mode where it behaves like an IME: fork. See the project sample for details. This has worked quite well for me and is similar to many of the "PIN entry" fake IMEs you see in banking apps.
您可以使用
inputType="phone"
,但是在这种情况下,您将必须处理多个,
或.
存在,因此需要进行额外的验证将是必要的。You could use
inputType="phone"
, however in that case you would have to deal with multiple,
or.
being present, so additional validation would be necessary.我对 KOTLIN 的修复
遇到了同样的错误,我用以下方法修复了它:
并且效果很好。 !但!在三星键盘上,不显示分隔符,因此您无法输入十进制数字。
所以我必须通过检查是否使用三星键盘来解决这个问题:
但是你仍然有“。”作为小数点分隔符。因此,如果分隔符是逗号,则必须用逗号替换点:
但是,您必须检查是否没有人在 EditText 字段中键入更多“,”。这可以通过正则表达式来完成。
我的整个解决方案:
xml文件:
如果你想使用号码,请确保获得正确的格式:
My fix for KOTLIN
I came across the same bug, which i fixed with:
and this works quite fine. !BUT! on Samsung Keyboards, the separator is NOT shown, so you cannot type in decimal numbers.
so i had to fix this issue with checking, if Samsung Keyboard is used:
But then you still have the "." as decimal separator. Therefore you have to replace the dot with comma, if the separator is comma:
But then you have to check that nobody types more "," in the EditTextfield. This can be done with a Regex.
My whole solution:
xml file:
If you want to use the number, make sure to get the right format:
Android 有一个内置的数字格式化程序。
您可以将其添加到
EditText
以允许小数点和逗号:android:inputType="numberDecimal"
和android:digits="0123456789.,"
然后在代码中的某个位置,当用户单击保存时或输入文本后(使用听众)。
Android has a built in number formatter.
You can add this to your
EditText
to allow decimals and commas:android:inputType="numberDecimal"
andandroid:digits="0123456789.,"
Then somewhere in your code, either when user clicks save or after text is entered (use a listener).
我决定仅在编辑时将逗号更改为点。这是我棘手且相对简单的解决方法:
someDecimalFormatter 将使用逗号或点取决于区域设置
I decided to change comma to dot only while editing. Here is my tricky and relative simple workaround:
someDecimalFormatter will use comma or dot depends on Locale
我的解决方案是:
在主要活动中:
字符分隔符 =DecimalFormatSymbols.getInstance().getDecimalSeparator();
textViewPitchDeadZone.setKeyListener(DigitsKeyListener.getInstance("0123456789" +分隔符));
在xml文件中:
<代码>
android:imeOptions="flagNoFullscreen"
android:inputType="numberDecimal"
我将 editText 中的双精度数作为字符串。
My solution is:
In main activity:
char separator =DecimalFormatSymbols.getInstance().getDecimalSeparator();
textViewPitchDeadZone.setKeyListener(DigitsKeyListener.getInstance("0123456789" + separator));
In xml file:
android:imeOptions="flagNoFullscreen"
android:inputType="numberDecimal"
and I took the double in the editText as a String.
8 年多过去了,我很惊讶这个问题还没有解决...
我在这个简单的问题上遇到了困难,因为@Martin 得到最多支持的答案允许输入多个分隔符,即用户可以输入“12,,,,,,12,1,,21,2,”< br>
另外,第二个问题是,在某些设备上,数字键盘上不显示逗号(或需要多次按点按钮)
这是我的解决方案,它解决了上述问题和让用户输入“.”和 ',',但在 EditText 中他将看到与当前语言环境相对应的唯一小数分隔符:
以及文本观察器:
It's more than 8 years passed and I am surprised, this issue isn't fixed yet...
I struggled with this simple issue since the most upvoted answer by @Martin lets typing multiple separators, i.e. user can type in "12,,,,,,12,1,,21,2,"
Also, the second concern is that on some devices comma is not shown on the numerical keyboard (or requires multiple pressing of a dot button)
Here is my workaround solution, which solves the mentioned problems and lets user typing '.' and ',', but in EditText he will see the only decimal separator which corresponds to current locale:
And the text watcher:
简单的解决方案,制作自定义控件。 (这是在 Xamarin android 中制作的,但应该可以轻松移植到 java)
Simple solution, make a custom control. (this is made in Xamarin android but should port easily to java)
我必须想出一个解决办法,它由不同的答案组成。
这将允许使用“,”或“.”。最多一位小数
这是我的 editText:
和我处理特定正则表达式的类:
I had to come up with a work around on my side that it consisted of a mix of different answers.
This will allow for "," or "." for up to one decimal
This is my editText:
and my class to handle specific regex:
我不知道为什么你的答案这么复杂。如果 SDK 中存在错误,您必须覆盖它或绕过它。
我选择了第二种方法来解决这个问题。如果将字符串格式化为
Locale.ENGLISH
,然后将其放入EditText
(即使是空字符串)。示例:String.format(Locale.ENGLISH,"%.6f", yourFloatNumber);
追求该解决方案,您的结果与显示的键盘兼容。然后浮点数和双精度数以典型的编程语言方式工作,使用点而不是逗号。
I don't know why your answers are so complicated. If there's a bug in SDK you must override it or go around.
I have chosen the second way to solve that problem. If you format your string as
Locale.ENGLISH
and then put it to theEditText
(even as an empty string). Example:String.format(Locale.ENGLISH,"%.6f", yourFloatNumber);
Chasing that solution your result are compatible with the shown keyboard. Then float and double numbers work in typical for programming languages manner with dot instead of comma.
我认为这个解决方案比这里写的其他解决方案简单:
这样当你按“.”时在软键盘中没有任何反应;只允许使用数字和逗号。
I think this solution is less complex than the others written here:
This way when you press the '.' in the soft keyboard nothing happens; only numbers and comma are allowed.
解决方法(直到 Google 修复此错误)是使用
EditText
和android:inputType="numberDecimal"
和android:digits="0123456789.,"< /代码>。
然后使用以下 afterTextChanged 将 TextChangedListener 添加到 EditText:
A workaround (until Google fix this bug) is to use an
EditText
withandroid:inputType="numberDecimal"
andandroid:digits="0123456789.,"
.Then add a TextChangedListener to the EditText with the following afterTextChanged: