Comma doesnt work on Samsung Android Keyboard

Dar*_*mar 5 android samsung-mobile kotlin android-studio

I set InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_CLASS_NUMBER to my EditText and i want to use comma for decimal separator. So i set digits "0123456789.," to EditText.

editText.keyListener = DigitsKeyListener.getInstance("0123456789.,")
Run Code Online (Sandbox Code Playgroud)

I set a TextWatcher on EditText to handle user input. When i clicked comma(",") on Android emulator keyboard, it is working as expected but if i get build on my phone, which has Samsung Keyboard, comma key is disabled and doesnt work. I searched so much but i couldn't find a way.

Any idea with this problem?

在此处输入图片说明

小智 0

现在已经 2022 年了,这个问题仍然存在(至少在三星设备上):)

因此,我通过向EditText添加TextChangedListener并检查最后输入的字符是否等于(特定于国家/地区的)千位分隔符来解决此问题。如果是这样,我将其替换为特定于国家/地区的小数分隔符:

 public class EditTextTausenderErsetzer implements TextWatcher{
 ...
    @Override
    public void afterTextChanged(Editable editable) 
    {
      // determine country specific separators
      // char thousandsSep = .., char commaSep =..

      int comma_index = editable.toString().indexOf(commaSep);
        if(comma_index == -1) {  // no comma so far, thus there might be a "." which shall be a comma separator
         if (editable.toString().indexOf(thousandsSep) != -1) {
            if (editable.toString().indexOf(thousandsSep, (editable.length()-1)) == (editable.length() - 1)) {
                    editable.delete(editable.length() - 1, editable.length() - 1);
                    editable.insert(editable.length(), commaSep + "");

                }
            }
        }
        // ...
        // do other stuff like setting automatically thousands separators

        
Run Code Online (Sandbox Code Playgroud)

如果您的应用程序管理千位分隔符,并且您不允许用户设置这些分隔符,则此解决方案有效。