【问题标题】:How to only allow positive numbers in an EditText如何在 EditText 中只允许正数
【发布时间】:2014-10-24 14:59:10
【问题描述】:

我有一个 TextWatcher,它可以在所有 EditTexts 长度()都为 != 0 时启用一个按钮。我现在想添加一个功能,以确保数字为正数。我尝试在另一个 if() 中放置一个新的 if() 来检查是否 >0 但由于某种原因它不起作用。

所以我需要确保所有 EditText 不为空并且已输入正数,然后启用该数字。

这就是我所拥有的,

    public TextWatcher localWatcher = 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) {
    }

    // When the text is edited, I want to check if all textViews are filled
    @Override
    public void afterTextChanged(Editable s) {

        // Check if any of the TextViews are == 0, if so the button remains disabled
        if      (et1local.getText().toString().length() == 0
                || et2local.getText().toString().length() == 0
                || et3local.getText().toString().length() == 0
                || et4local.getText().toString().length() == 0
                || et5local.getText().toString().length() == 0
                || et6local.getText().toString().length() == 0
                || et7local.getText().toString().length() == 0
                || et8local.getText().toString().length() == 0
                || et9local.getText().toString().length() == 0) {

            if(et1local){




        localCalculateButton.setEnabled(false);
            }

        }

        // When all are filled enable the button
        else {
            localCalculateButton.setEnabled(true);
        }
    }
};

这很好,但是如何检查数字是否为正,任何帮助都非常感谢。

【问题讨论】:

  • 把“-”换成“”怎么样?

标签: java android textwatcher


【解决方案1】:

你应该使用EditText的attr:

android:digits="0123456789."
android:inputType="number"

http://developer.android.com/reference/android/widget/TextView.html#attr_android:digits

因此用户将只能输入不带减号的数字。

更新: 类似的东西,以防止从 0 开始的第一个符号:

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable edt) {
            if (edt.length() == 1 && edt.toString().equals("0"))
                editText.setText("");
        }

        // ...
    });

【讨论】:

  • 嗨 Vito 我已经实现了这个,但我不想在第一个数字空间中放置 0,因为这将是负数。
  • @Getek99 如果你同时设置android:digits="0123456789." android:inputType="numberDecimal" 那么它也应该只接受格式化数字,(并删除前导0)
  • 你用 android:inputType="numberDecimal" 试过 Doomsknight 的建议了吗?
  • 如果 android:inputType="numberDecimal" 没有帮助,那么您必须在 TextWatcher 中处理它。如果我的回答对你有帮助,请采纳 =)
  • 是的,我试过 android:digits="0123456789。" android:inputType="numberDecimal"
【解决方案2】:

对于每个EditText,您必须这样做:

if (Integer.parseInt(et1local.getText().toString()) > 0)

执行一个函数以确保它是一个数字

private boolean isPositive(EditText et)
{
    try
    {
        return Integer.parseInt(et.getText().toString()) > 0;
    }
    catch (Exception e)
    {
        return false;
    }
}

像这样使用它:

if (et1local.getText().toString().length() == 0 || !isPositive(et1local)
 || et2local.getText().toString().length() == 0 || !isPositive(et2local)
// [...]

【讨论】:

    【解决方案3】:

    这比 ToYonos 的回答有点啰嗦,但可能有助于理解原理。它还简化了你的 if 块。

    private boolean isEditTextEmptyOrNegative(EditText editText) {
        // check if editText is empty
        if (editText.getText().toString().length() == 0)
            return true;
    
        // get text from editText
        String text = editText.getText().toString();
    
        try {
            // try to parse text to int
            int textInt = Integer.valueOf(text);
    
            // if int is > 0, return false (not empty or negative), else return true (int is negative)
            if (textInt > 0)
                return false;
            else
                return true;
        } catch (NumberFormatException nfe) {
            // catch NumberFormatException, text entered is not a valid int, treat it as empty (you may want to handle differently)
            return true;
        }
    }
    

    你的 if 块会是这样的:

    if (isEditTextEmptyOrNegative(et1local) || isEditTextEmptyOrNegative(et2local) || ... )
    

    【讨论】:

      【解决方案4】:

      只需删除 onTextChanged 中的减号即可。

      public void onTextChanged(CharSequence s, int start, int before,
              int count) {
      
            et1Local.setText(et1local.getText().toString().replace"-",""));
            .....
            et9Local.setText(et9local.getText().toString().replace("-".""));
      }
      

      【讨论】:

        【解决方案5】:

        对我来说,只需设置 android:inputType="number"(没有 android:digits 部分)就可以了。
        而且我在将前导零转换为负数方面没有问题,就像您似乎有的那样(我正在使用Integer.valueOf(binding.myEditText.getEditableText().toString()) 获取我的输入)。

        【讨论】:

          猜你喜欢
          • 2016-07-20
          • 2022-01-22
          • 1970-01-01
          • 1970-01-01
          • 2019-06-15
          • 1970-01-01
          • 2014-02-15
          • 2020-01-09
          • 2016-06-30
          相关资源
          最近更新 更多