带有numberDecimal输入的小数分隔符逗号(',')在EditText中输入

时间:2010-09-29 12:26:12

标签: android android-edittext

inputType中的numberDecimal EditText使用点'。'作为小数分隔符。在欧洲,通常使用逗号','代替。即使我的语言环境设置为德语,小数分隔符仍然是'。'

有没有办法将逗号作为小数分隔符?

24 个答案:

答案 0 :(得分:95)

解决方法(在Google修复此错误之前)使用EditText android:inputType="numberDecimal"android:digits="0123456789.,"

然后使用以下afterTextChanged将TextChangedListener添加到EditText:

public void afterTextChanged(Editable s) {
    double doubleValue = 0;
    if (s != null) {
        try {
            doubleValue = Double.parseDouble(s.toString().replace(',', '.'));
        } catch (NumberFormatException e) {
            //Error
        }
    }
    //Do something with doubleValue
}

答案 1 :(得分:26)

此处提供的“数字”解决方案的变体:

char separator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
input.setKeyListener(DigitsKeyListener.getInstance("0123456789" + separator));

考虑区域设置分隔符。

答案 2 :(得分:17)

遵循EditText的代码货币掩码($ 123,125.155)

Xml布局

  <EditText
    android:inputType="numberDecimal"
    android:layout_height="wrap_content"
    android:layout_width="200dp"
    android:digits="0123456789.,$" />

代码

EditText testFilter=...
testFilter.addTextChangedListener( new TextWatcher() {
        boolean isEdiging;
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        @Override public void afterTextChanged(Editable s) {
            if(isEdiging) return;
            isEdiging = true;

            String str = s.toString().replaceAll( "[^\\d]", "" );
            double s1 = Double.parseDouble(str);

            NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
            ((DecimalFormat)nf2).applyPattern("$ ###,###.###");
            s.replace(0, s.length(), nf2.format(s1));

            isEdiging = false;
        }
    });

答案 3 :(得分:15)

这是Android SDK中的known bug。 唯一的解决方法是创建自己的软键盘。您可以找到实施here的示例。

答案 4 :(得分:6)

如果您以编程方式实例化EditText,那么Martins的答案将无效。我继续修改了API 14中包含的DigitsKeyListener类,以允许逗号和句点作为小数分隔符。

要使用此功能,请致电setKeyListener()上的EditText,例如

// Don't allow for signed input (minus), but allow for decimal points
editText.setKeyListener( new MyDigitsKeyListener( false, true ) );

但是,您仍然必须在TextChangedListener中使用Martin的诀窍,用逗号替换逗号

import android.text.InputType;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.NumberKeyListener;
import android.view.KeyEvent;

class MyDigitsKeyListener extends NumberKeyListener {

    /**
     * The characters that are used.
     *
     * @see KeyEvent#getMatch
     * @see #getAcceptedChars
     */
    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 MyDigitsKeyListener[] sInstance = new MyDigitsKeyListener[4];

    @Override
    protected char[] getAcceptedChars() {
        return mAccepted;
    }

    /**
     * Allocates a DigitsKeyListener that accepts the digits 0 through 9.
     */
    public MyDigitsKeyListener() {
        this(false, false);
    }

    /**
     * Allocates a DigitsKeyListener that accepts the digits 0 through 9,
     * plus the minus sign (only at the beginning) and/or decimal point
     * (only one per field) if specified.
     */
    public MyDigitsKeyListener(boolean sign, boolean decimal) {
        mSign = sign;
        mDecimal = decimal;

        int kind = (sign ? SIGN : 0) | (decimal ? DECIMAL : 0);
        mAccepted = CHARACTERS[kind];
    }

    /**
     * Returns a DigitsKeyListener that accepts the digits 0 through 9.
     */
    public static MyDigitsKeyListener getInstance() {
        return getInstance(false, false);
    }

    /**
     * Returns a DigitsKeyListener that accepts the digits 0 through 9,
     * plus the minus sign (only at the beginning) and/or decimal point
     * (only one per field) if specified.
     */
    public static MyDigitsKeyListener getInstance(boolean sign, boolean decimal) {
        int kind = (sign ? SIGN : 0) | (decimal ? DECIMAL : 0);

        if (sInstance[kind] != null)
            return sInstance[kind];

        sInstance[kind] = new MyDigitsKeyListener(sign, decimal);
        return sInstance[kind];
    }

    /**
     * Returns a DigitsKeyListener that accepts only the characters
     * that appear in the specified String.  Note that not all characters
     * may be available on every keyboard.
     */
    public static MyDigitsKeyListener getInstance(String accepted) {
        // TODO: do we need a cache of these to avoid allocating?

        MyDigitsKeyListener dim = new MyDigitsKeyListener();

        dim.mAccepted = new char[accepted.length()];
        accepted.getChars(0, accepted.length(), dim.mAccepted, 0);

        return dim;
    }

    public int getInputType() {
        int contentType = InputType.TYPE_CLASS_NUMBER;
        if (mSign) {
            contentType |= InputType.TYPE_NUMBER_FLAG_SIGNED;
        }
        if (mDecimal) {
            contentType |= InputType.TYPE_NUMBER_FLAG_DECIMAL;
        }
        return contentType;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {
        CharSequence out = super.filter(source, start, end, dest, dstart, dend);

        if (mSign == false && mDecimal == false) {
            return out;
        }

        if (out != null) {
            source = out;
            start = 0;
            end = out.length();
        }

        int sign = -1;
        int decimal = -1;
        int dlen = dest.length();

        /*
         * Find out if the existing text has '-' or '.' characters.
         */

        for (int i = 0; i < dstart; i++) {
            char c = dest.charAt(i);

            if (c == '-') {
                sign = i;
            } else if (c == '.' || c == ',') {
                decimal = i;
            }
        }
        for (int i = dend; i < dlen; i++) {
            char c = dest.charAt(i);

            if (c == '-') {
                return "";    // Nothing can be inserted in front of a '-'.
            } else if (c == '.' ||  c == ',') {
                decimal = i;
            }
        }

        /*
         * If it does, we must strip them out from the source.
         * In addition, '-' must be the very first character,
         * and nothing can be inserted before an existing '-'.
         * Go in reverse order so the offsets are stable.
         */

        SpannableStringBuilder stripped = null;

        for (int i = end - 1; i >= start; i--) {
            char c = source.charAt(i);
            boolean strip = false;

            if (c == '-') {
                if (i != start || dstart != 0) {
                    strip = true;
                } else if (sign >= 0) {
                    strip = true;
                } else {
                    sign = i;
                }
            } else if (c == '.' || c == ',') {
                if (decimal >= 0) {
                    strip = true;
                } else {
                    decimal = i;
                }
            }

            if (strip) {
                if (end == start + 1) {
                    return "";  // Only one character, and it was stripped.
                }

                if (stripped == null) {
                    stripped = new SpannableStringBuilder(source, start, end);
                }

                stripped.delete(i - start, i + 1 - start);
            }
        }

        if (stripped != null) {
            return stripped;
        } else if (out != null) {
            return out;
        } else {
            return null;
        }
    }
}

答案 5 :(得分:5)

您可以使用以下解决方法将逗号作为有效输入包括在内: -

通过XML:

<EditText
    android:inputType="number"
    android:digits="0123456789.," />

<强>编程:

EditText input = new EditText(THE_CONTEXT);
input.setKeyListener(DigitsKeyListener.getInstance("0123456789.,"));

这样Android系统会显示数字键盘并允许输入逗号。希望这能回答这个问题:)

答案 6 :(得分:2)

对于Mono(Droid)解决方案:

decimal decimalValue = decimal.Parse(input.Text.Replace(",", ".") , CultureInfo.InvariantCulture);

答案 7 :(得分:2)

您可以将以下内容用于不同的区域设置

private void localeDecimalInput(final EditText editText){

    DecimalFormat decFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault());
    DecimalFormatSymbols symbols=decFormat.getDecimalFormatSymbols();
    final String defaultSeperator=Character.toString(symbols.getDecimalSeparator());

    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if(editable.toString().contains(defaultSeperator))
                editText.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
            else
                editText.setKeyListener(DigitsKeyListener.getInstance("0123456789" + defaultSeperator));
        }
    });
}

答案 8 :(得分:1)

获取本地化您的输入用途:

char sep = DecimalFormatSymbols.getInstance().getDecimalSeparator();

然后添加:

textEdit.setKeyListener(DigitsKeyListener.getInstance("0123456789" + sep));

比不要忘记用“。”替换“,”。所以Float或Double可以毫无错误地解析它。

答案 9 :(得分:1)

恕我直言,解决这个问题的最佳方法是使用InputFilter。这里有一个很好的要点DecimalDigitsInputFilter。那么你可以:

editText.setInputType(TYPE_NUMBER_FLAG_DECIMAL | TYPE_NUMBER_FLAG_SIGNED | TYPE_CLASS_NUMBER)
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789,.-"))
editText.setFilters(new InputFilter[] {new DecimalDigitsInputFilter(5,2)});

答案 10 :(得分:1)

您可以执行以下操作:

DecimalFormatSymbols d = DecimalFormatSymbols.getInstance(Locale.getDefault());
input.setFilters(new InputFilter[] { new DecimalDigitsInputFilter(5, 2) });
input.setKeyListener(DigitsKeyListener.getInstance("0123456789" + d.getDecimalSeparator()));

然后你可以使用输入过滤器:

    public class DecimalDigitsInputFilter implements InputFilter {

Pattern mPattern;

public DecimalDigitsInputFilter(int digitsBeforeZero, int digitsAfterZero) {
    DecimalFormatSymbols d = new DecimalFormatSymbols(Locale.getDefault());
    String s = "\\" + d.getDecimalSeparator();
    mPattern = Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}+((" + s + "[0-9]{0," + (digitsAfterZero - 1) + "})?)||(" + s + ")?");
}

@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

    Matcher matcher = mPattern.matcher(dest);
    if (!matcher.matches())
        return "";
    return null;
}

}

答案 11 :(得分:0)

Android内置数字格式化程序。

您可以将其添加到EditText以允许小数和逗号: android:inputType="numberDecimal"android:digits="0123456789.,"

然后在代码中的某个位置,用户单击“保存”或输入文本后(使用侦听器)。

// Format the number to the appropriate double
try { 
    Number formatted = NumberFormat.getInstance().parse(editText.getText().toString());
    cost = formatted.doubleValue();
} catch (ParseException e) {
    System.out.println("Error parsing cost string " + editText.getText().toString());
    cost = 0.0;
}

答案 12 :(得分:0)

我决定在编辑时将逗号更改为点。这是我棘手且相对简单的解决方法:

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            EditText editText = (EditText) v; 
            String text = editText.getText().toString();
            if (hasFocus) {
                editText.setText(text.replace(",", "."));
            } else {
                if (!text.isEmpty()) {
                    Double doubleValue = Double.valueOf(text.replace(",", "."));
                    editText.setText(someDecimalFormatter.format(doubleValue));
                }
            }
        }
    });

someDecimalFormatter将使用逗号或点取决于Locale

答案 13 :(得分:0)

我不知道为什么你的答案如此复杂。如果SDK中存在错误,您必须覆盖它或绕过它。

我选择了解决这个问题的第二种方法。如果将字符串格式化为Locale.ENGLISH,然后将其放入EditText(即使是空字符串)。例如:

String.format(Locale.ENGLISH,"%.6f", yourFloatNumber);

追逐该解决方案,您的结果与显示的键盘兼容。然后浮点数和双数字在编程语言方式中通常使用点而不是逗号。

答案 14 :(得分:0)

我的解决方案是:

  • 在主要活动中:

    <body> <div class="container"> <div class="register"> <form method="post" action="userregistration.php"> <h1>Sign up</h1> <label for="email">E-mailadres:</label><br> <div class="infobox"> <i class="fa fa-envelope"></i> <input class="text-input" type="email" name="mail" placeholder="example:emailname@mail.com"><br> <span class="error"><?php echo $emailErr;?></span><br> </div> <label for="password">Wachtwoord:</label><span class="passrequirements"> (minimaal 5 karakters)</span><br> <div class="infobox"> <i class="fa fa-key"></i> <input class="text-input" type="password" name="password" placeholder="******"><br> <span class="error"><?php echo $passErr;?></span><br> </div> <label for="password">Herhaal wachtwoord:</label><br> <div class="infobox"> <i class="fa fa-key"></i> <input class="text-input" type="password" name="passwordrepeat" id="password" placeholder="******"><br> <span class="error"><?php echo $repeatpassErr;?></span><br> </div> <label for="country">Provincie:</label><br> <div class="infobox"> <i class="fa fa-flag"></i> <select name="province"> <option value="DR">Drenthe</option> <option value="FL,">Flevoland</option> <option value="FR">Friesland</option> <option value="GE">Gelderland</option> <option value="GR">Groningen</option> <option value="LI">Limburg</option> <option value="NB">Noord-Brabant</option> <option value="NH">Noord-Holland</option> <option value="OV">Overijssel</option> <option value="UT">Utrecht</option> <option value="ZE">Zeeland</option> <option value="ZH">Zuid-Holland</option> </select><br> </div> <button type="submit" class="signup-button" name="register">Registreer</button><br> <a class="login-link" href="userlogin.php">Ik heb al een account</a> </form> </div> </div> </body>

  • 在xml文件中: char separator =DecimalFormatSymbols.getInstance().getDecimalSeparator(); textViewPitchDeadZone.setKeyListener(DigitsKeyListener.getInstance("0123456789" + separator));

我把editText中的double作为String。

答案 15 :(得分:0)

我可以确认,建议的修复程序不适用于Samsung IME(至少在S6和S9上)以及LG。它们仍然显示点作为小数点分隔符,而不考虑语言环境。切换到Google的IME可以解决此问题,但对于大多数开发人员来说几乎不是一个选择。

这些键盘在Oreo中也未得到修复,因为这是三星和/或LG必须进行的修复,然后甚至可以推向他们的旧手机。

我改而分叉了number-keyboard project并添加了一种模式,其行为类似于IME:fork。有关详细信息,请参见项目示例。这对我来说效果很好,类似于您在银行应用中看到的许多“ PIN输入”假IME。

Sample app screenshot

答案 16 :(得分:0)

已经过去8年多了,我很惊讶,这个问题尚未解决...
我对这个简单的问题感到困惑,因为 @Martin最受支持的答案允许键入多个分隔符,即用户可以键入“ 12 ,,,,,, 12,1,,21,2,” < br /> 另外,第二个问题是在某些设备上逗号不会显示在数字键盘上(或需要多次按下点按钮)

这是我的解决方法,它解决了上述问题并允许用户键入'。和',',但在EditText中,他将看到唯一与当前语言环境相对应的小数点分隔符:

editText.apply { addTextChangedListener(DoubleTextChangedListener(this)) }

还有文本观察器:

  open class DoubleTextChangedListener(private val et: EditText) : TextWatcher {

    init {
        et.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
        et.keyListener = DigitsKeyListener.getInstance("0123456789.,")
    }

    private val separator = DecimalFormatSymbols.getInstance().decimalSeparator

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        //empty
    }

    @CallSuper
    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        et.run {
            removeTextChangedListener(this@DoubleTextChangedListener)
            val formatted = toLocalizedDecimal(s.toString(), separator)
            setText(formatted)
            setSelection(formatted.length)
            addTextChangedListener(this@DoubleTextChangedListener)
        }
    }

    override fun afterTextChanged(s: Editable?) {
        // empty
    }

    /**
     * Formats input to a decimal. Leaves the only separator (or none), which matches [separator].
     * Examples:
     * 1. [s]="12.12", [separator]=',' -> result= "12,12"
     * 2. [s]="12.12", [separator]='.' -> result= "12.12"
     * 4. [s]="12,12", [separator]='.' -> result= "12.12"
     * 5. [s]="12,12,,..,,,,,34..,", [separator]=',' -> result= "12,1234"
     * 6. [s]="12.12,,..,,,,,34..,", [separator]='.' -> result= "12.1234"
     * 7. [s]="5" -> result= "5"
     */
    private fun toLocalizedDecimal(s: String, separator: Char): String {
        val cleared = s.replace(",", ".")
        val splitted = cleared.split('.').filter { it.isNotBlank() }
        return when (splitted.size) {
            0 -> s
            1 -> cleared.replace('.', separator).replaceAfter(separator, "")
            2 -> splitted.joinToString(separator.toString())
            else -> splitted[0]
                    .plus(separator)
                    .plus(splitted.subList(1, splitted.size - 1).joinToString(""))
        }
    }
}

答案 17 :(得分:0)

简单的解决方案,进行自定义控件。 (这是在Xamarin android中制作的,但应轻松移植到Java)

public class EditTextDecimalNumber:EditText
{
    readonly string _numberFormatDecimalSeparator;

    public EditTextDecimalNumber(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        InputType = InputTypes.NumberFlagDecimal;
        TextChanged += EditTextDecimalNumber_TextChanged;
        _numberFormatDecimalSeparator = System.Threading.Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator;

        KeyListener = DigitsKeyListener.GetInstance($"0123456789{_numberFormatDecimalSeparator}");
    }

    private void EditTextDecimalNumber_TextChanged(object sender, TextChangedEventArgs e)
    {
        int noOfOccurence = this.Text.Count(x => x.ToString() == _numberFormatDecimalSeparator);
        if (noOfOccurence >=2)
        {
            int lastIndexOf = this.Text.LastIndexOf(_numberFormatDecimalSeparator,StringComparison.CurrentCulture);
            if (lastIndexOf!=-1)
            {
                this.Text = this.Text.Substring(0, lastIndexOf);
                this.SetSelection(this.Text.Length);
            }

        }
    }
}

答案 18 :(得分:0)

这里的所有其他帖子都存在重大漏洞,因此,此解决方案将:

  • 基于区域强制使用逗号或句号,不允许您键入相反的逗号或句点。
  • 如果EditText以某个值开头,则会根据需要替换正确的分隔符。

在XML中:

<EditText
    ...
    android:inputType="numberDecimal" 
    ... />

类变量:

private boolean isDecimalSeparatorComma = false;

在onCreate中,找到当前语言环境中使用的分隔符:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    NumberFormat nf = NumberFormat.getInstance();
    if (nf instanceof DecimalFormat) {
        DecimalFormatSymbols sym = ((DecimalFormat) nf).getDecimalFormatSymbols();
        char decSeparator = sym.getDecimalSeparator();
        isDecimalSeparatorComma = Character.toString(decSeparator).equals(",");
    }
}

也onCreate,如果要加载当前值,请使用此命令进行更新:

// Replace editText with commas or periods as needed for viewing
String editTextValue = getEditTextValue(); // load your current value
if (editTextValue.contains(".") && isDecimalSeparatorComma) {
    editTextValue = editTextValue.replaceAll("\\.",",");
} else if (editTextValue.contains(",") && !isDecimalSeparatorComma) {
    editTextValue = editTextValue.replaceAll(",",".");
}
setEditTextValue(editTextValue); // override your current value

还可以创建,添加侦听器

editText.addTextChangedListener(editTextWatcher);

if (isDecimalSeparatorComma) {
    editText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));
} else {
    editText.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
}

editTextWatcher

TextWatcher editTextWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }

    @Override
    public void afterTextChanged(Editable s) {
        String editTextValue = s.toString();

        // Count up the number of commas and periods
        Pattern pattern = Pattern.compile("[,.]");
        Matcher matcher = pattern.matcher(editTextValue);
        int count = 0;
        while (matcher.find()) {
            count++;
        }

        // Don't let it put more than one comma or period
        if (count > 1) {
            s.delete(s.length()-1, s.length());
        } else {
            // If there is a comma or period at the end the value hasn't changed so don't update
            if (!editTextValue.endsWith(",") && !editTextValue.endsWith(".")) {
                doSomething()
            }
        }
    }
};

doSomething()示例,转换为标准时间以进行数据处理

private void doSomething() {
    try {
        String editTextStr = editText.getText().toString();
        if (isDecimalSeparatorComma) {
            editTextStr = editTextStr.replaceAll(",",".");
        }
        float editTextFloatValue = editTextStr.isEmpty() ?
                0.0f :
                Float.valueOf(editTextStr);

        ... use editTextFloatValue
    } catch (NumberFormatException e) {
        Log.e(TAG, "Error converting String to Double");
    }
}

答案 19 :(得分:0)

您可以使用inputType="phone",但是在这种情况下,您将不得不处理多个,.,因此有必要进行额外的验证。

答案 20 :(得分:0)

我对KOTLIN的修复

我遇到了相同的错误,已通过以下方式修复:

val separator = DecimalFormatSymbols.getInstance().decimalSeparator
mEditText.keyListener = DigitsKeyListener.getInstance("0123456789$separator")

,并且效果很好。 !但!在Samsung Keyboards上,未显示分隔符,因此您不能输入十进制数字。

因此,如果使用三星键盘,我必须通过检查解决此问题:

    val x = Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
    if (x.toLowerCase().contains("samsung")) {}
  

但是您仍然有“。”作为小数点分隔符。因此,如果分隔符为逗号,则必须用逗号替换点:

val separator: Char = DecimalFormatSymbols.getInstance().decimalSeparator
 
  if (separator == ',') {
     mEditText.addTextChangedListener(object : TextWatcher {
                        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
    
                        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
    
                        override fun afterTextChanged(s: Editable?) {
    
                            if (!s.isNullOrEmpty()) {
                                
                                    if (s.toString().contains(".")) {
                                        val replaced = s.toString().replace('.', separator)
                                        mEditText.setText(replaced)
                                        mEditText.setSelection(replaced.length)
                                    }
    
                                
                            }
                        }
                    })
                }

但是随后您必须检查是否没有人在EditTextfield中键入更多“,”。可以使用正则表达式来完成。

我的整个解决方案:

val x = Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
  if (x.toLowerCase().contains("samsung")) {
         val Number_REGEX: Pattern =  Pattern.compile("^([1-9])*([.,]{1}[0-9]{0,10})?$")
         val separator: Char = DecimalFormatSymbols.getInstance().decimalSeparator
         if (separator == ',') {
            mEditText.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit

                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit

                override fun afterTextChanged(s: Editable?) {

                    if (!s.isNullOrEmpty()) {
                        val matcherMail = Number_REGEX.matcher(s.toString())
                        if (!matcherMail.matches()) {
                            val length: Int = s.length

                            s.delete(length - 1, length);
                        } else {
                            if (s.toString().contains(".")) {
                                val replaced = s.toString().replace('.', separator)
                                mEditText.setText(replaced)
                                mEditText.setSelection(replaced.length)
                            }

                        }
                    }
                }
            })
        }
    } else {
        val separator = DecimalFormatSymbols.getInstance().decimalSeparator
        mEditText.keyListener = DigitsKeyListener.getInstance("0123456789$separator")
       
    }

xml文件:

 <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/tEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Input"
        android:inputType="numberDecimal"
        android:imeOptions="actionDone"/>

如果要使用数字,请确保使用正确的格式:

val x = NumberFormat.getInstance().parse(mEditText.text.toString()).toDouble()

答案 21 :(得分:0)

我有一个解决方案,允许用户输入点和逗号(如果在键盘上可用),但是仅显示区域设置默认分隔符。此外,它不允许用户输入超过1个分隔符。引用EditText或无限循环都没有问题。它是此线程中几个适合我需要的答案的组合。

与接受的答案一样,相应地配置EditText

android:inputType="numberDecimal"
android:digits="0123456789.,"

然后在EditText上设置自定义TextWatcher:

myEditText.addTextChangedListener(FlexibleDecimalSeparatorTextWatcher())

并包含自定义TextWatcher:

import android.text.Editable
import android.text.SpannableStringBuilder
import android.text.TextWatcher
import android.widget.EditText
import java.text.DecimalFormatSymbols

/**
 * The [FlexibleDecimalSeparatorTextWatcher] allows the user to input both the comma (,) and dot (.) as a decimal separator,
 * and will then automatically convert each entered separator into the locale default separator.
 * If the user were to enter multiple separators - every separator but the first will be removed.
 *
 * To provide comma and dot support, set the [EditText] inputType to 'numberDecimal' and its digits to '0123456789.,'.
 */
class FlexibleDecimalSeparatorTextWatcher : TextWatcher {
companion object {
    private val DECIMAL_SEPARATORS = listOf('.', ',')
    private val LOCALE_DEFAULT_DECIMAL_SEPARATOR = DecimalFormatSymbols.getInstance().decimalSeparator
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}

override fun afterTextChanged(s: Editable?) {
    if (s != null) {
        val textWithConvertedSeparators = convertSeparatorsToLocaleDefault(s.toString())
        val textWithoutMultipleSeparators = removeAdditionalSeparators(textWithConvertedSeparators)

        // Make the change if required. This only triggers one additional afterTextChanged call if there were changes.
        if(s.toString() != textWithoutMultipleSeparators) {
            s.replace(0, s.length, SpannableStringBuilder(textWithoutMultipleSeparators))
        }
    }
}

/**
 * This function converts all entered separators (in [DECIMAL_SEPARATORS]) to the [LOCALE_DEFAULT_DECIMAL_SEPARATOR].
 */
private fun convertSeparatorsToLocaleDefault(original: String): String {
    var result = original
    DECIMAL_SEPARATORS.forEach { separator ->
        if (separator != LOCALE_DEFAULT_DECIMAL_SEPARATOR && result.contains(separator)) {
            result = result.replace(separator, LOCALE_DEFAULT_DECIMAL_SEPARATOR)
        }
    }
    return result
}

/**
 * Strip out all separators but the first.
 * In this function we assume all separators are already converted to the locale default.
 */
private fun removeAdditionalSeparators(original: String): String {
    var result = original
    var separatorCount = result.count { c -> c == LOCALE_DEFAULT_DECIMAL_SEPARATOR }
    if(separatorCount > 1) {
        // We will reverse the text so we can keep stripping the last (first in reverse) separator off.
        var textReversed = result.reversed()
        val separatorRegex = Regex.fromLiteral(LOCALE_DEFAULT_DECIMAL_SEPARATOR.toString())
        while (separatorCount > 1) {
            textReversed = textReversed.replaceFirst(separatorRegex, "")
            separatorCount--
        }
        // And finally we reverse it back to the original order.
        result = textReversed.reversed()
    }
    return result
}

}

答案 22 :(得分:0)

我不得不想出一个解决办法,它由不同答案的混合组成。 这将允许“,”或“。”最多一位小数

这是我的编辑文本:

val separator: Char = DecimalFormatSymbols.getInstance().decimalSeparator
editTextBox.filters = arrayOf<InputFilter>(DecimalDigitsInputFilter(5, 1, separator))
editTextBox.keyListener = DigitsKeyListener.getInstance("0123456789$separator")

和我的班级来处理特定的正则表达式:

class DecimalDigitsInputFilter(
    digitsBeforeZero: Int, 
    digitsAfterZero: Int, 
    separator: Char
) : InputFilter {
    private val mPattern: Pattern =
        Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}+((\\$separator[0-9]{0," + (digitsAfterZero - 1) + "})?)||(\\$separator)?")

    override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
        val matcher = mPattern.matcher(dest)
        return if (!matcher.matches()) "" else null
    }
}

答案 23 :(得分:-1)

我认为这个解决方案不如其他人写的那么复杂:

false

这样当你按下“。”在软键盘中没有任何反应;只允许使用数字和逗号。