falopasensacion
falopasensacion

Reputation: 3

How set the string format in editText

I want the string being displayed in my EditText like as:

"AA-BB-11", but i need only put inside AABB11, it's possible ?

Upvotes: 0

Views: 887

Answers (1)

C. Bd
C. Bd

Reputation: 73

I think you have to implement TextWatcher. Try something like that :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText test = (EditText) findViewById(R.id.editText) 
        test.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

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

    }

    @Override
    public void afterTextChanged(Editable text) {
      if (text.length() == 2 || text.length() == 5) {
        text.append('-');
      }
    }

See : https://developer.android.com/reference/android/text/TextWatcher.html

EDIT :

You can see full code here : https://stackoverflow.com/a/28043245/6450234.

You have just to change text.length() == 3 || text.length() == 7 by text.length() == 2 || text.length() == 5

Upvotes: 2

Related Questions