Android-逗号作为数字键盘上的小数点分隔符 [英] Android - Comma as decimal separator on Numeric Keyboard
问题描述
我们需要一个用于EditText
的数字键盘.键盘应根据设备的所选区域设置使用小数点分隔符.我们通过将自定义DigitsKeyListener
设置为EditText
We need to have a numeric keyboard for an EditText
. The Keyboard should have decimal separator based on the device's selected locale. We implemented this by setting the custom DigitsKeyListener
to the EditText
public class NumericDigitsKeyListener extends DigitsKeyListener {
@Override
protected char[] getAcceptedChars() {
char[] acceptedCharacters = null;
acceptedCharacters = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
new DecimalFormatSymbols(Locale.getDefaultLocale()).getDecimalSeparator()
return acceptedCharacters;
}
/**
* @see android.text.method.DigitsKeyListener#getInputType()
*/
public int getInputType() {
return InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL;
}
以上内容对于大多数设备似乎都可以正常工作,但对于Samsung Galaxy S-II,软键盘的键盘上没有逗号.该设备的swype键盘显示逗号,但默认键盘上没有逗号.
The above seems to work fine for most of the devices however for Samsung Galaxy S-II, the softkeyboard does not have comma in the keyboard.Th swype keyboard of the device displays the comma, but the default one does not.
我已经尝试过此处
有没有一种方法可以强制所有设备在数字键盘上都带有逗号(适用时,甚至总是)?
Is there a way I can enforce all the devices to have comma (when applicable or even always) to be there on numeric keyboard?
推荐答案
我认为您的代码与使用以下代码存在相同的问题:
I think that your code has the same problem as using:
android:inputType ="numberDecimal" android:digits ="0123456789,"
android:inputType="numberDecimal" android:digits="0123456789,"
(除了您的代码更通用)
(except that your code is more generic)
问题-据我了解,当 inputType 为 numberDecimal 时,某些键盘只是不尊重 digits 属性.这是一个已记录的错误,也是我发现避免该错误的唯一方法此行为是使用 inputType ="text" 并设置输入过滤器以限制字符.当然,键盘不适用于数字,但找不到更好的解决方案.过滤器将是这样的:
The problem - as I understand, is that some keyboards just don't respect digits property when inputType is numberDecimal. This is a documented bug and the only way I found to avoid this behaviour is to use inputType="text" and set a input filter to restrict characters. Of course, the keyboard isn't proper for numbers but couldn't find a better solution. The filter would be something like this:
mEditText.setFilters(new InputFilter[] { new DecimalInputFilter() });
public class DecimalInputFilter implements InputFilter {
private static final String ALLOWED_CHARS = "0123456789,";
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source instanceof SpannableStringBuilder) {
final SpannableStringBuilder sourceAsSpannableBuilder = (SpannableStringBuilder)source;
for (int i = end - 1; i >= start; i--) {
final char currentChar = source.charAt(i);
if (!StringUtils.contains(ALLOWED_CHARS, currentChar)) {
sourceAsSpannableBuilder.delete(i, i+1);
}
}
return source;
} else {
final StringBuilder filteredStringBuilder = new StringBuilder();
for (int i = 0; i < end; i++) {
final char currentChar = source.charAt(i);
if (StringUtils.contains(ALLOWED_CHARS, currentChar)) {
filteredStringBuilder.append(currentChar);
}
}
return filteredStringBuilder.toString();
}
}
}
这篇关于Android-逗号作为数字键盘上的小数点分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!