0

I'm trying to handle the Enter button on Android soft keyboard. I tried what it's said in this post also from StackOverflow, but with no luck.

I have two EditText fields: one that asks for user name and the other for the user email. When I complete the username, I want to perform a Next action and go down to the next EditText.

This is my .xml file:

<EditText android:layout_width="fill_parent" android:layout_height="wrap_content"
              android:id="@+id/text_user_name" android:layout_marginTop="20dp"
              android:layout_marginLeft="10dp" android:layout_marginRight="10dp"
              android:layout_weight="1"
              android:imeOptions="actionNext"
              android:inputType="text"
              android:nextFocusDown="@+id/text_user_email"
              android:hint="name"/>
    <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@id/text_user_email"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_weight="1"
                android:imeOptions="actionDone"
                android:hint="***@email.com"/>

This is my OnEditorActionListener:

return new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if(actionId == EditorInfo.IME_ACTION_DONE && event.getKeyCode() == KeyEvent.KEYCODE_ENTER){

                if (!finished)
                {
                    // Check user information
                    if (CheckUserInfo()) finished = true;
                }
                return true;
            }
            else
            if (actionId == EditorInfo.IME_ACTION_NEXT && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                Toast.makeText(getApplicationContext(), "go a line down", Toast.LENGTH_SHORT).show();
                return true;
            }
            else
            {
                return false;
            }
        }
    };

I tried also to change the visible text for the Enter button, to look like a Next for the first EditText and as a Done for the final EditText like this:

textUserName.setImeActionLabel("Next", KeyEvent.KEYCODE_ENTER);
.... 
textUserEmail.setImeActionLabel("Done", KeyEvent.KEYCODE_ENTER);

But this part is not working either.

Can anybody help me understand why I can't make anything work? :_( Thanks!

Community
  • 1
  • 1
Sonhja
  • 8,230
  • 20
  • 73
  • 131

2 Answers2

1

You can use

     android:imeActionLabel="your text here"
     android:imeOptions="actionNext"

for next imeoption

and

    android:imeActionLabel="your text here"
    android:imeOptions="actionSend"

for the done button

Sripathi
  • 1,760
  • 15
  • 20
0

you can do this way too

editText.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

             if (actionId == KeyEvent.KEYCODE_ENTER) { 
                 doSomeThing();

             }


            return false;
        }
    });