【问题标题】:Decimal separator key not visible when DigitsKeyListener is set设置 DigitsKeyListener 时小数分隔键不可见
【发布时间】:2019-02-03 03:42:18
【问题描述】:

我希望允许用户根据地区输入带有小数点分隔符(逗号或点)的数字。

现在我使用DigitsKeyListener 来启用逗号作为某些区域(例如波兰、德国)的分隔符。

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

这段代码写在

如果软件键盘是Gboard 或其他第三方键盘,那么一切都很好,用户可以使用逗号或点作为分隔符。

但当用户使用普通键盘并设置了DigitsKeyListener 时,并非所有键都可见并且用户无法添加分隔符。

这是我的 EditText 代码:

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInput"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    app:hintTextAppearance="@style/Lorin.Theme.TextInputLayout"
    app:layout_constraintLeft_toRightOf="@id/imageInfo"
    app:layout_constraintRight_toLeftOf="@id/buttonHistoryNumeric">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/editTextValue"
        style="@style/Lorin.EditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="flagNavigateNext"
        android:inputType="number|numberSigned|numberDecimal"
        android:maxLines="1"
        tools:hint="Range"/>
</android.support.design.widget.TextInputLayout>


val separator = DecimalFormatSymbols.getInstance().decimalSeparator
//editTextValue.keyListener = DigitsKeyListener.getInstance("-0123456789$separator")

注释DigitsKeyListener 的行时,分隔符的键可见,但唯一可用的分隔符是

您知道如何在普通键盘上使用逗号作为分隔符吗?

【问题讨论】:

    标签: kotlin android kotlin android-edittext


    【解决方案1】:

    问题是DigitsKeyListener does not specify the decimal/signed flag 在使用 getInstance 和一串可接受的字符时。由于 KeyListener 覆盖了 EditText 的输入类型,因此在设置 KeyListener 后,您的 EditText 现在具有 inputType 编号,而不是 numberDecimal 或 numberSigned。

    由于 DigitsKeyListener 非常有用,一个简单的解决方法是简单地委托给它,但使用正确的输入类型。在 Kotlin 中,这非常简单:

    class DecimalSignedDigitsKeyListener(digitsKeyListener: DigitsKeyListener) : 
        KeyListener by digitsKeyListener {
    
      override fun getInputType() =
        InputType.TYPE_CLASS_NUMBER or 
        InputType.TYPE_NUMBER_FLAG_DECIMAL or 
        InputType.TYPE_NUMBER_FLAG_SIGNED
    }
    
    
    val separator = DecimalFormatSymbols.getInstance().decimalSeparator
    val digitsKeyListener = DigitsKeyListener.getInstance("<your digits>")
    editTextValue.keyListener = DecimalSignedDigitsKeyListener(digitsKeyListener)
    

    我建议始终接受 '.'除了任何特定于区域设置的分隔符之外,因为并非所有键盘实际上都会显示逗号或其他符号。

    【讨论】:

      【解决方案2】:

      我曾经遇到过同样的问题。三星设备上的普通键盘太可怕了……没有办法用逗号显示数字键盘。

      我发现的最佳解决方案是通过 TextWatcher 将点替换为逗号。
      但前提是默认语言环境的小数点分隔符是逗号。

      digits 中允许点(.) 和逗号(,) 并在XML 中将inputType 设置为numberDecimal

      <EditText
              android:id="@+id/editText_price"
              android:layout_marginLeft="8dp"
              android:layout_marginStart="8dp"
              android:digits="1234567890,."
              android:imeOptions="actionDone"
              android:inputType="numberDecimal" />
      

      然后将TextWatcher 添加到这个editText:

      val separator = DecimalFormatSymbols.getInstance().decimalSeparator
      if (separator == ',') {
          editText_price.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.toString().contains(".")) {
                      val replaced = s.toString().replace('.', separator)
                      editText_price.setText(replaced)
                      editText_price.setSelection(replaced.length)
                  }
              }
          })
      }
      

      然后要获取字符串的实际数量,请使用:

      try {
          val price = DecimalFormat.getInstance().parse(editTextPrice.text.toString());
      } catch (e: ParseException) {
          e.printStackTrace()
          editText_price.setError(getString(R.string.error))
      }
      

      【讨论】:

        【解决方案3】:

        公共类 MainActivity 扩展 Activity {

        private EditText editText;
        private String blockCharacterSet = "~#^|$%&*!";
        
        private InputFilter filter = new InputFilter() {
        
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        
                if (source != null && blockCharacterSet.contains(("" + source))) {
                    return "";
                }
                return null;
            }
        };
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            editText = (EditText) findViewById(R.id.editText);
            editText.setFilters(new InputFilter[] { filter });
        }
        

        }

        【讨论】:

          猜你喜欢
          • 2017-06-07
          • 1970-01-01
          • 2011-12-12
          • 1970-01-01
          • 1970-01-01
          • 2021-05-12
          • 2013-12-21
          • 1970-01-01
          相关资源
          最近更新 更多