0

I have multiple textBoxes. Each of them has a TextWatcher attached to it. Now I wanted to set an OnKeyListener for the last mandatory textBox, so I could handle Enter key. When Enter key is pressed in theis textBox, I want to set the text of this textBox to a default value, then I want to save data from all textBoxes, clear all textBoxes and set the focus to the first textbox, so the user can enter new data. However, obviously I cannot effectively requestFocus on any other TextBox or Button in the onKey-eventhandler of the TextBox.OnKeyListener.

myLastTextBox.setOnKeyListener( new EditText.OnKeyListener() {  
    @Override
    public boolean onKey(View arg0, int arg1, KeyEvent event) {
        if (event!= null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN){
            Button  nextB = (Button) findViewById(R.id.myBtn);
            nextB.performClick(); //the function called validates all fields, 
                                  //saves the data to lists, sets all editText 
                                  //to "", requests focus to the first editText.                
            res= true;
        }
        else if (event!= null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER &&  event.getAction() == KeyEvent.ACTION_UP) {
            res=true; //consume also the keyUp event of the enter key.
        }
        return res;
    }
});

When the program returns from the onKey-event handler, the focus is still in the same textBox. Any help appreciated.

EDIT: Ok, I wanted to simplify the question, but maybe I made it too easy. When Enter is pressed, I not only want to set the focus, but I want to call a function, that reads, saves, clears all textBoxes and then sets the focus to the first editText.

2
  • 1
    have you tried setting the next focus key in the XML layout? I think it might do what you're attempting out of the box
    – panini
    Jan 16, 2014 at 21:04
  • Thanks, tried this out, but that does not work in this case.
    – AndyZ
    Jan 16, 2014 at 21:21

1 Answer 1

1

So, you should look into IME actions. Let's say you have a layout with three EditText views:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <EditText
        android:id="@+id/username"
        android:hint="@string/username_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nextFocusDown="@+id/password"
        android:imeOptions="actionNext" />

    <EditText
        android:id="@+id/password"
        android:hint="@string/password_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nextFocusDown="@+id/confirm_password"
        android:inputType="text|textPassword"
        android:imeOptions="actionNext" />

    <EditText
        android:id="@+id/confirm_password"
        android:hint="@string/confirm_password_hint"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text|textPassword"
        android:imeOptions="actionGo" />

</LinearLayout>

Just by itself, this will handle movement between fields automatically and provide you with a "Next" button, instead of an enter key (for standard input methods).

Now, to intercept the last key (which will be "Go"):

final EditText username = (EditText) findViewById(R.id.username);
final EditText password = (EditText) findViewById(R.id.password);
final EditText confirm = (EditText) findViewById(R.id.confirm_password);

confirm.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (EditorInfo.IME_ACTION_GO == actionId) {
            String un = username.getText().toString();
            String pass = password.getText().toString();
            String conf = confirm.getText().toString();

            username.setText(null);
            password.setText(null);
            confirm.setText(null);

            /* Save your stuff */
            // ...

            username.requestFocus();
            return true;
        } else {
            return false;
        }
    }
});
2
  • Thank you very much. I will have a deeper look into IME actions and will give this a try in the evening. I would be glad if someone could point out what concept prevents the focus to be set to another element in the onKey event of the onKeyListener.
    – AndyZ
    Jan 17, 2014 at 6:23
  • 1
    Thank you very much, this solves my problem and even the text of the Enter button becomes more readable.
    – AndyZ
    Jan 17, 2014 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.