Search code examples
javaandroidedittextpreference

EditTextPreference validate upon entry of text


I have a problem when trying to validate text upon entry of text, I have a hint that a user should follow 'GXW-999' is an example, this is my code:

   `public final static String[] PATTERNS = {"[A-Z]{1}" , "[A-Z]{2}" , "[A-Z]{3}" , "[A-Z]{3}-", "[A-Z]{3}-[0-9]{1}", "[A-Z]{3}-[0-9]{2}", "[A-Z]{3}-[0-9]{3}" };`
  ....
  ` mTextPreference.getEditText().setHint("GXW-999");
    mTextPreference.getEditText().setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
    mTextPreference.getEditText().addTextChangedListener(new AfterTextChanged(etext){

        @Override
        public void validate(EditText v, Editable s) {
            // TODO Auto-generated method stub
            Log.i(TAG, "after text change " + s.toString());
            String text = s.toString();
            int length = text.length();
            if(length <= 0) return;
            if(!Pattern.matches(PATTERNS[length - 1], text)){
                text = s.delete(length - 1, length).toString();
                v.setText(text);
            }
        }

    });`

My abstract textwatcher

`private abstract class AfterTextChanged implements TextWatcher{

    private EditText v;
    public AfterTextChanged(EditText v){
        this.v = v;
    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        validate(v , s);
    }

    public abstract void validate(EditText v , Editable s);

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

    }

}`

The problem im getting at is when typing the last number 999 and if i exceed to 1 more number 9999 im getting an error whilst the GXW if i exceed it to 1 more character it wont have any error.

I hope my question is concise and easy to understand. I beg of help because im actually dont know other ways around after i have read this and it said you cant get the ok button in EditTExtPreference.

If this wont work , is there any other work around?


Solution

  • lol im a noob, i got it.. just change my

            @Override
            public void validate(EditText v, Editable s) {
                // TODO Auto-generated method stub
                Log.i(TAG, "after text change " + s.toString());
                String text = s.toString();
                int length = text.length();
                if(length <= 0) return;
                if(!Pattern.matches(PATTERNS[length - 1], text)){
                    text = s.delete(length - 1, length).toString();
                    v.setText(text);
                }
            }
    

    to

            @Override
            public void validate(EditText v, Editable s) {
                // TODO Auto-generated method stub
                Log.i(TAG, "after text change " + s.toString());
                String text = s.toString();
                int length = text.length();
                if(length <= 0) return;
                int max_length = Math.min(length - 1, PATTERNS.length - 1);
                if(!Pattern.matches(PATTERNS[max_length], text)){
                    text = s.delete(length - 1, length).toString();
                    Log.i(TAG, "the text " + text);
                    v.setText(text);
                }
            }
    

    and its working..