0

I have 4 edit texts in an Activity, with input limit of 3 characters.
I want to implement behavior as when user types 3 characters in previous edit text - next edit should get focus automatically. On the contrary: when user starts deleting from the end of last edit text - user can delete chars in all previous fields without manual selecting each edit text.
What is the easiest way to implement this?

I can only think of using keyListenners for each EditText, check text length and switch focus when it equals to max value.

Is this a correct way or there are any other solutions?

Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41
Androider
  • 407
  • 2
  • 7
  • 17
  • Show the code you are working with now, including the layout please. – Blake Yarbrough Jan 21 '16 at 14:12
  • Use TextWatcher, you can also checkout the counting solution here http://stackoverflow.com/questions/4310525/counting-chars-in-edittext-changed-listener – Shamas S Jan 21 '16 at 14:13
  • Thank you. And how to better switch from one to another edittext? – Androider Jan 21 '16 at 14:16
  • Looks like a duplicate, http://stackoverflow.com/questions/19442622/how-to-change-the-focus-to-next-edit-text-in-android – sat Jan 21 '16 at 14:27
  • There are many ways of doing it, you have to get the text size of the current input and focus to the next editText. Calculating entered text size can be done in many ways, For example, InputFilters, textwatcher. You can also extend an edit text and create your own view implementation which has feature to focus next when end of the edit text is reached. – sat Jan 21 '16 at 14:32

3 Answers3

1

I think it's the best way too.

I would have try something like:

firstText.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(CharSequence s) {}
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){
        if(s.toString().length() == 3){
            sedoncText.setFocusableInTouchMode(true);
            sedoncText.requestFocus();
        }
    }
});

I didn't test it so maybe some of the functions are not exactly spelled correctly.

Hope it helps.

Nicolas Cortell
  • 659
  • 4
  • 16
1

You can use a TextWatcher for this job. A short code snippet:

editTextA.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                   // implement your logic here
                   // e.g. Set cursor to the end of another Textfield:

                   if (s.toString().length() > 2){
                      int position = editTextB.length();
                      editTextB.setSelection(position);
                    }
                }

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

                    // TODO Auto-generated method stub
                }

                @Override
                public void afterTextChanged(Editable s) {

                    // TODO Auto-generated method stub
                }
});

For your task, every EditText needs his own TextWatcher (Including the specifig logic).

UPDATE:

A short (untested) idea how to switch EditText when deleting:

private boolean isEditTextVirgin;
isEditTextVirgin = true;
editTextB.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // Check if the TextView is filled before editing
                // if yes then save this information for later
                if (s.toString().length() > 0){
                    isEditTextVirgin = false;
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                // Check if the EditText is empty after a edit and if it was filled before
                // if both yes then jump the the previous EditText
                if (s.toString().length() == 0 && isEditTextVirgin == false){
                    int position = editTextA.length();
                    editTextA.setSelection(position);
                    // if you want you can reset your variable
                    // isEditTextVirgin = true;
                }
            }
});

You are saving a status (if the edittext ever was filled) in a member variable.

skymedium
  • 767
  • 7
  • 26
  • and how to manage characters deletion so when I move to previous field when delete all chars in the last field? – Androider Jan 21 '16 at 15:40
1

In EditText add

android:maxLength="3" 

This will not allow more than 3 character input.

And to focus on next edittext please refer below link

Link: Move focus to next edittext

Community
  • 1
  • 1
Raghu Nagaraju
  • 3,278
  • 1
  • 18
  • 25