Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set next EditText focused and editable on KEY_DOWN

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;
            }
        });
like image 792
user1462299 Avatar asked Jun 21 '12 07:06

user1462299


People also ask

How do I change the focus to next text in android?

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; } });

How do you make EditText not editable and clickable?

Make EditText non editable in Android To use this just set android:inputType="none" and it will become non editable.

Can you set text in EditText?

In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file.


2 Answers

Besides adding the + in front of id:

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

I also had to add:

android:imeOptions="actionNext"
like image 118
lenooh Avatar answered Oct 26 '22 10:10

lenooh


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

worked for me

like image 25
Matthias H Avatar answered Oct 26 '22 12:10

Matthias H