13

I've got a layout with two editexts. I need to set the second one focused and editable when the enter key is hit on the first one. I've got code for setting focus but I can't start typing when the second one gets the focus.

PS I also need edittext to be exactly one line height without possibility of making addtional rows. It works as well. pss android 2.3

XML code:

<EditText
            android:id="@+id/email"
            style="@style/profileLoginInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:hint="@string/email" android:ems="10" android:lines="1" android:maxLines="1" android:ellipsize="end">

            <requestFocus />
        </EditText>

<EditText
            android:id="@+id/password"
            style="@style/profileLoginInput"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:ems="10"
            android:inputType="textPassword" android:hint="@string/password">

Java code :

((EditText) findViewById(R.id.email)).setOnKeyListener(new OnKeyListener(){
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event)
            {
                if (event.getAction() == KeyEvent.ACTION_DOWN)
                {
                    switch (keyCode)
                    {
                        case KeyEvent.KEYCODE_DPAD_CENTER:
                        case KeyEvent.KEYCODE_ENTER:
                        ((EditText) findViewById(R.id.password)).requestFocus();
                        return true;
                        default:
                            break;
                    }
                }
                return false;
            }
        });
Pratik Butani
  • 56,749
  • 54
  • 254
  • 407
user1462299
  • 4,099
  • 5
  • 20
  • 30

5 Answers5

23

You're overthinking this. You can specify the order of the EditText inputs right in the XML without doing anything in Java.

Use the android:nextFocusDown param to specify which field you'd like to get focus when the user hits the enter key.

Paresh Mayani
  • 125,853
  • 70
  • 238
  • 294
Yevgeny Simkin
  • 27,096
  • 37
  • 131
  • 232
12
android:inputType="text"
android:nextFocusDown="@+id/..."

worked for me

Matthias H
  • 1,290
  • 12
  • 18
4

android:nextFocusDown Worked fine for me But removed the following Method only then its work:

editText1.setOnEditorActionListener(new EditText.OnEditorActionListener() {

 @Override

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      if (actionId == EditorInfo.IME_ACTION_DONE ||   
              actionId == EditorInfo.IME_ACTION_NEXT) {
         editText2.requestFocus();
         return false;
      }

      return false;
    }
});
Darin Kolev
  • 3,368
  • 13
  • 31
  • 45
4

Besides adding the + in front of id:

android:inputType="text"
android:nextFocusDown="@+id/..."

I also had to add:

android:imeOptions="actionNext"
lenooh
  • 9,712
  • 5
  • 56
  • 49
2

It seems to be bugged when running this code on the emulator. However, it works on phones

user1462299
  • 4,099
  • 5
  • 20
  • 30