Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the focus to next edit text in android?

enter image description here

The User can enter only one digit in the edit text. if he enters the value in edtText1, I want the cursor automatically moves to edtText2 and so on. The user can able to edit the text which he/she has entered already. I tried the following way.

    edtPasscode1.setOnKeyListener(new OnKeyListener() {          @Override         public boolean onKey(View v, int keyCode, KeyEvent event) {             // TODO Auto-generated method stub             if (edtPasscode1.getText().length() == 1)                 edtPasscode2.requestFocus();             return false;         }     });      edtPasscode2.setOnKeyListener(new OnKeyListener() {          @Override         public boolean onKey(View v, int keyCode, KeyEvent event) {             // TODO Auto-generated method stub             if (edtPasscode2.getText().length() == 1)                 edtPasscode3.requestFocus();             return false;         }     });      edtPasscode3.setOnKeyListener(new OnKeyListener() {          @Override         public boolean onKey(View v, int keyCode, KeyEvent event) {             // TODO Auto-generated method stub             if (edtPasscode3.getText().length() == 1)                 edtPasscode4.requestFocus();             return false;         }     }); 

If the user edit the text, The cursor moves to some other editTexts and not working as desired. How can i achieve the above?

like image 665
Rethinavel Avatar asked Oct 18 '13 06:10

Rethinavel


People also ask

How do you delete focus on edit text?

It's fine if it worked for you, I solved it with android:focusable="true" android:focusableInTouchMode="true" in the parent RelativeLayout. This answer totally worked for me, removing focus of editText AND closing keyboard.

How do I turn off edit text on android?

What should I set to disable EditText? It's possible to preserve both the style of the view and the scrolling behaviour. To disable an EditText while keeping this properties, just use UI. setReadOnly(myEditText, true) from this library.

How do I make text editable in android?

If you set android:editable="true" you can access the TextView via the D-pad, or you could add android:focusableInTouchMode="true" to be able to gain focus on touch. The problem is you cannot modify the existing text, and you cannot move the cursor. The text you write just gets added before the existing text.


1 Answers

Try TextWatcher instead of onKeyListener

B'coz if want to edit your password, in that case TextWatcher will give you more method to dealt with..

Edited:-

StringBuilder sb=new StringBuilder();           edtPasscode1.addTextChangedListener(new TextWatcher() {              public void onTextChanged(CharSequence s, int start, int before, int count) {                  // TODO Auto-generated method stub                  if(sb.length()==0&edtPasscode1.length()==1)                  {                      sb.append(s);                      edtPasscode1.clearFocus();                      edtPasscode2.requestFocus();                      edtPasscode2.setCursorVisible(true);                   }              }               public void beforeTextChanged(CharSequence s, int start, int count,                      int after) {                   if(sb.length()==1)                  {                       sb.deleteCharAt(0);                   }               }               public void afterTextChanged(Editable s) {                  if(sb.length()==0)                  {                       edtPasscode1.requestFocus();                  }               }          }); 

Hope this work.

like image 122
Amit Gupta Avatar answered Sep 23 '22 10:09

Amit Gupta