3

I have an anctivity with 6 EditText... the activity is working good, but I need to click on each EditText to fill it...

How can I make enter key change the focus to another EditText?

What I've already tried

1. I already setted OnKeyListener on every EditText.

switch (v.getId()) {
    case R.id.editText1:
        activity.findViewById(R.id.editText2).requestFocus();
        return true;
    case R.id.editText2:
        activity.findViewById(R.id.editText3).requestFocus();
        return true;
    case R.id.editText3:
        activity.findViewById(R.id.editText4).requestFocus();
        return true;
            .
            .
            .
}

But, when I click on enter key, before the focus change to another EditText, a new line is create on previous EditText.

2. I've add android:singleLine="true" to every EditText. It isn't work on EditText with inputType="number"

BrunoTS
  • 171
  • 2
  • 17
  • Could you please try adding "android:maxLines="1"" too? in my experience there's difference having both of them even if one is "deprecated". I think your option 1 is the way to go in this case. – Tassadar Oct 24 '18 at 21:29
  • You could also try with "setOnEditorActionListener(...", in the KeyEvent param listen for event.getKeyCode() == KeyEvent.KEYCODE_ENTER and return true when this condition is true, that should stop EditText to do hes default behaviour – Tassadar Oct 24 '18 at 21:37
  • Possible duplicate of [Move to another EditText when Soft Keyboard Next is clicked on Android](https://stackoverflow.com/questions/17989733/move-to-another-edittext-when-soft-keyboard-next-is-clicked-on-android) – Ali Khaki Oct 24 '18 at 21:41
  • ~Tassadar: Setting maxLines="1" a new line is created, but edittext isn't resized. – BrunoTS Oct 24 '18 at 21:59

2 Answers2

5

You can add imeOptions attribute for EditText's in your xml:

android:imeOptions="actionNext"

But not forget to specify inputType: For example, android:inputType="text"


For more details check this question - Move to another EditText when Soft Keyboard Next is clicked on Android

Denysole
  • 3,903
  • 1
  • 20
  • 28
0

Try this code :

it working fine in otp screen i am using four edit text in this activity follow these step

  • i am using TextWatcher class like this

    /*Textwatcher class*/
    public class GenericTextWatcher implements TextWatcher {
    private View view;
    public GenericTextWatcher(View view) {
    this.view = view;
    }
    
     @Override
      public void afterTextChanged(Editable editable) {
    // TODO Auto-generated method stub
    String text = editable.toString();
    switch(view.getId()) {
        case R.id.edit_otp1:
            if(text.length()==1)
                edit_otp2.requestFocus();
            break;
        case R.id.edit_otp2:
            if(text.length()==1)
                edit_otp3.requestFocus();
            break;
        case R.id.edit_otp3:
            if(text.length()==1)
                edit_otp4.requestFocus();
            break;
        case R.id.edit_otp4:
            break;
       }
      }
    
    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    }
    
    @Override
     public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    }
     }
    

Use textwatcher in edittext otp activity like this

/*call textwatcher class*/
    edit_otp1.addTextChangedListener(new GenericTextWatcher(edit_otp1));
    edit_otp2.addTextChangedListener(new GenericTextWatcher(edit_otp2));
    edit_otp3.addTextChangedListener(new GenericTextWatcher(edit_otp3));
    edit_otp4.addTextChangedListener(new GenericTextWatcher(edit_otp4));

it works for me please try.

Android Geek
  • 8,956
  • 2
  • 21
  • 35