无法显示,作为android软键盘中的小数分隔符
我试图在android软键盘上显示“.”而不是“.”作为十进制分隔符,但我无法实现同样的效果。下面是我尝试的代码。 我还编写了一个扩展NumberKeyListener的类,并在为EditText设置keylistener时使用了这个类。但仍然不起作用无法显示,作为android软键盘中的小数分隔符,android,Android,我试图在android软键盘上显示“.”而不是“.”作为十进制分隔符,但我无法实现同样的效果。下面是我尝试的代码。 我还编写了一个扩展NumberKeyListener的类,并在为EditText设置keylistener时使用了这个类。但仍然不起作用 public class DecimalDigitsInputFilter extends NumberKeyListener { private static final char[][] CHARACTERS = new cha
public class DecimalDigitsInputFilter extends NumberKeyListener {
private static final char[][] CHARACTERS = new char[][] {
new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' },
new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-' },
new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ',' },
new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '.', ',' },
};
private char[] mAccepted;
private boolean mSign;
private boolean mDecimal;
private static final int SIGN = 1;
private static final int DECIMAL = 2;
private static DecimalDigitsInputFilter[] sInstance = new DecimalDigitsInputFilter[4];
@Override
protected char[] getAcceptedChars() {
return mAccepted;
}
public DecimalDigitsInputFilter() {
this(false, false);
}
public DecimalDigitsInputFilter(boolean sign, boolean decimal) {
mSign = sign;
mDecimal = decimal;
int kind = (sign ? SIGN : 0) | (decimal ? DECIMAL : 0);
mAccepted = CHARACTERS[kind];
}
public static DecimalDigitsInputFilter getInstance() {
return getInstance(false, false);
}
public static DecimalDigitsInputFilter getInstance(boolean sign, boolean decimal) {
int kind = (sign ? SIGN : 0) | (decimal ? DECIMAL : 0);
if (sInstance[kind] != null)
return sInstance[kind];
sInstance[kind] = new DecimalDigitsInputFilter(sign, decimal);
return sInstance[kind];
}
public static DecimalDigitsInputFilter getInstance(String accepted) {
DecimalDigitsInputFilter dim = new DecimalDigitsInputFilter();
dim.mAccepted = new char[accepted.length()];
accepted.getChars(0, accepted.length(), dim.mAccepted, 0);
return dim;
}
@Override
public int getInputType() {
return InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL
| InputType.TYPE_NUMBER_FLAG_SIGNED;
}
}
像这样在课堂上使用
mWeightEditText.setKeyListener(new DecimalDigitsInputFilter(false, true));
在android软键盘上不能用逗号代替圆点 您可以将
NumberKeyListener
扩展为接受“、”和“.”,如下所示:
@Override
public int getInputType() {
return InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL
| InputType.TYPE_NUMBER_FLAG_SIGNED;
}
此外,您还可以在EditText
上设置TextWatcher
,它将postertextchanged()
方法中的所有“.”替换为“,”
您可以尝试的另一种方法是:
xml:
这样,Android将显示数字键盘并允许输入逗号,如果用户按下“.”按钮,则不会发生任何事情。(您可以在xml和代码中添加“.”,这样用户也可以输入“.”。我尝试了所有方法,但仍然不起作用。我编辑了我的问题,请看相同的。
@Override
public int getInputType() {
return InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL
| InputType.TYPE_NUMBER_FLAG_SIGNED;
}
<EditText
android:inputType="number"
android:digits="0123456789.," />
EditText input = new EditText(THE_CONTEXT);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));