How to Change Focuse in Edit text after entering 2 Character

764 views Asked by At

Hello I have Difficulty to Change focus between two edit text. this task perform in Android 4.2 very well but Same task done in Samsung Duos Android 4.0.1 it's not working please give me Suggestions. Thank You

My Code is:

first_number_from.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (first_number_from.getText().length() == 2)
                    second_number_from.requestFocus();

                return false;
            }
        }); 
2

There are 2 answers

1
Luciano On BEST ANSWER

You could add a text changed listener

first_number_from.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start,int before, int count)  {
                if(first_number_from.getText().toString().length() == 2) {
                    second_number_from.requestFocus();
                }
            }
            public void beforeTextChanged(CharSequence s, int start,
                            int count, int after) {}

            public void afterTextChanged(Editable s) {

            }

        });
0
Linga On

Try TextWatcher instead of onKeyListener

@Override
public void afterTextChanged(Editable s)
{
if (first_number_from.getText().toString().length() == 2)
{
     second_number_from.requestFocus();
}
}