0

this is often asked like here Android emulator : insert negative number?

I have setup an edittext:

       <EditText
        android:id="@+id/edWminLevel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:inputType="numberDecimal|numberSigned"
        android:text="0" />

when trying inserting a negative number, I press "-" and it inserts "-", but then it won't let me inserting any other digit! It works only by inserting the "-" after the complete number was inserted.

is this a BUG?

3
  • 1
    i can do this with your code. Commented Feb 17, 2014 at 12:24
  • so it must be the phone? it is Android 4.2.1 Commented Feb 17, 2014 at 12:25
  • thanks for reply, but at the end it was my fault! Commented Feb 17, 2014 at 12:44

1 Answer 1

1

the answer is simple: it is not a BUG it is the Programmer!

I just publish this in order to help others.

I had an InputFilter applied to the EditText ...

EdWMin.setFilters(new InputFilter[] { new DecimalDigitsInputFilter(7, 2) });

here the fix allowing signs in front of digits

    public class DecimalDigitsInputFilter implements InputFilter {

    Pattern mPattern;

    public DecimalDigitsInputFilter(int digitsBeforeZero,int digitsAfterZero) {
        mPattern=Pattern.compile("[-+]?[0-9]{0," + (digitsBeforeZero-1) + "}+((\\.[0-9]{0," + (digitsAfterZero-1) + "})?)||(\\.)?");
    }



    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

            Matcher matcher=mPattern.matcher(dest);       
            if(!matcher.matches())
                return "";
            return null;
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.