简体   繁体   中英

How to only allow positive numbers in an EditText

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.

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

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.

You should use the attr of EditText:

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

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:

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

        // ...
    });

For each EditText , you have to do this :

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)
// [...]

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

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 (isEditTextEmptyOrNegative(et1local) || isEditTextEmptyOrNegative(et2local) || ... )

Just remove minus signs in onTextChanged .

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

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

For me, simply setting android:inputType="number" (without the android:digits part) did the trick just fine.
And I had no problems with leading zeros converting to negative numbers as you seemed to have (I'm getting my inputs with Integer.valueOf(binding.myEditText.getEditableText().toString()) ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM