EditText OnKeyListener with setFocus not working as per expected

738 Views Asked by At

I am using an Android device with physical hardware keyboard.
I have two EditTexts (Part No and Qty) and Two Buttons (Reset and Save) on screen.

After user key in Qty, they want to press Enter key from physical keyboard to trigger "Save" function.
My current Save function will save the data to file and clear the data on screen and set the focus to Part No.

It seems like my onKeyListener didn't return True once it was consumed and it trigger another Enter key action to move my cursor to Qty field (by right, it should stay in Part No field).
I thought return True will indicate this Enter key has been consumed, am I understanding correct?

My two EditText are put directly under ConstraintLayout (not under any sub-layout).
Any idea on where my code goes wrong?

Below is my piece of code for layout.

<EditText
    android:id="@+id/etPart"
    android:layout_width="200dp"
    android:maxLength="20"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:maxLines="1"
    android:inputType="text"
    android:imeOptions="actionNext"
    app:layout_constraintLeft_toRightOf="@+id/tvPart"
    app:layout_constraintTop_toTopOf="parent"/>

 <EditText
    android:id="@+id/etQty"
    android:layout_width="100dp"
    android:maxLength="5"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:inputType="number"
    android:maxLines="1"
    android:imeOptions="actionDone"
    app:layout_constraintLeft_toRightOf="@+id/tvQty"
    app:layout_constraintTop_toBottomOf="@id/etPart"
    android:nextFocusDown="@id/etPart"/>

Below is my Java code.
This is in onCreate method.

etQty.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if(i == KeyEvent.KEYCODE_ENTER){
                saveData(null);
                // I tried to use return true here, sames result, no different.
            }
            return false;
        }
    });

This is the functions.

public void saveData(View view) {
    // check and save data to file logic.
    // ...... etc.
    mProductViewModel.insert(product);

    resetView(null);
}

 public void resetView(View view) {
    etPart.setText(null);
    etQty.setText(null);

    // I am expecting the focus will stay in Part No box but it is not. It will jump to Qty field.
    etPart.requestFocus();
}
2

There are 2 best solutions below

0
On BEST ANSWER

After checking with the specialist, I came to understand that I also need to handle Action Up (Key Up) too.

Below is the working code.

    etQty.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if(i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_UP){
                saveData(null);
                return true;
            }
            return false;
        }
    });
6
On

This is usually caused by soft keyboard, can you try this code?

etPart.requestFocus();
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.restartInput(etPart);
imm.showSoftInput(etPart, 0);