java 如何在 EditText 中只允许正数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26550616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:07:50  来源:igfitidea点击:

How to only allow positive numbers in an EditText

javaandroidtextwatcher

提问by Getek99

I have a TextWatcher that enables a button when all the EditTexts length() are != 0. I now want to add the ability to also make sure the number is positive. I tried putting a new if() inside the other if() to check if >0 but for some reason it doesn't work.

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

So what I need is to make sure all EditText are not empty and positive numbers have been entered then enable the number.

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

This is what I have,

这就是我所拥有的

    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);
        }
    }
};

This works fine but how to also check if the number is positive, any help wpuld be great thanks.

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

回答by Vito

You should use the attr of EditText:

您应该使用 EditText 的属性:

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

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

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

So user will be able enter only digits without minus.

所以用户将只能输入没有减号的数字。

UPDATED: Something like that, to prevent first sign from 0:

更新:类似的东西,以防止从 0 的第一个标志:

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

        // ...
    });

回答by ToYonos

For each EditText, you have to do this :

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

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

Do a function to ensure it's a number

做一个函数来确保它是一个数字

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

Use it like that :

像这样使用它:

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

回答by invertigo

This is a bit more long winded than ToYonos answer, but may help understanding the principles. It also simplifies your if block a bit.

这比 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;
    }
}

and your if block would be something like this:

你的 if 块将是这样的:

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

回答by greenapps

Just remove minus signs in onTextChanged.

只需删除onTextChanged.

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

      et1Local.setText(et1local.getText().toString().replace"-",""));
      .....
      et9Local.setText(et9local.getText().toString().replace("-".""));
}