【问题标题】:How to make Comma enterable into EditText only once?如何使逗号只能输入一次 EditText?
【发布时间】:2017-10-16 21:20:29
【问题描述】:

我已经做了很长一段时间的事情,但我就是无法完成。我的问题是我有一个 EditText,我希望用户能够在其中输入十进制数(例如 3,95 或 3.95)。我将 inputType 设置为 numberDecimal,并尝试将数字限制为“0123456789”。但它要么不让用户输入逗号,要么让用户输入它们的负载。我希望用户只能输入一个分隔符(无论是“。”还是“,”)。谁能帮我解决这个问题?

【问题讨论】:

  • 我已经在某事工作了很长一段时间,但我就是无法完成 [...] 没有提供代码。简而言之 StackOverflow。

标签: java android


【解决方案1】:

实时检查格式。

1. replace , => .
2. cast to double
if casting fails, go back to backuped string.

在布局xml:

<EditText
    android:id="@+id/editText"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:inputType="numberDecimal"
    android:digits="0123456789.,"/>

在java中

final EditText editText = (EditText) findViewById(R.id.editText);
        editText.addTextChangedListener(new TextWatcher() {
            String sBackup;

            @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) {
                double value;
                try {
                    if (editable.toString().equals("") == false) {
                        value = Double.valueOf(editable.toString().replace(',', '.'));
                        sBackup = editable.toString();
                    }
                } catch (Exception e) {
                    editText.setText(sBackup);
                    editText.setSelection(editText.getText().toString().length());
                }
            }
        });

【讨论】:

    【解决方案2】:

    首先根据语言环境获取分隔符。

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

    然后,设置 textwatcher 进行限制。

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

    【讨论】:

      【解决方案3】:

      您需要在编辑文本上设置输入过滤器。

       public static InputFilter getFilter() {
          return new InputFilter() {
              @Override
              public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                  for (int index = start; index < end; index++) {
                     // Filter here
                  }
                  return null;
              }
          };
      }
      
      
      
      et_Txt.setFilters(new InputFilter[]{getFilter()});
      

      然后根据您的要求验证每个字符输入。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-22
        相关资源
        最近更新 更多