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
  • 60,504
  • 58
  • 273
  • 437
user1462299
  • 4,247
  • 5
  • 22
  • 30

5 Answers5

24

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
  • 127,700
  • 71
  • 241
  • 295
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
12
android:inputType="text"
android:nextFocusDown="@+id/..."

worked for me

Matthias H
  • 1,300
  • 13
  • 19
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,401
  • 13
  • 31
  • 46
4

Besides adding the + in front of id:

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

I also had to add:

android:imeOptions="actionNext"
lenooh
  • 10,364
  • 5
  • 58
  • 49
2

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

user1462299
  • 4,247
  • 5
  • 22
  • 30