Keep your focus on edittext after pressing Enter

Asked

Viewed 83 times

1

I have a barcode reader that does the action of putting the text in edittext and pressing enter automatically, I implemented the function while reading but I would like the focus on edittext to continue. My code :

final EditText codBarras = (EditText) activity.findViewById(R.id.codigoBarras);
        codBarras.requestFocus();

        codBarras.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN) && (i == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    SQLiteDatabase dbr = banco.getReadableDatabase();
                    Cursor cursor = dbr.rawQuery("SELECT _id FROM produto WHERE codigo_barra = '"+codBarras.getText().toString()+"'", null);
                    if (cursor.getCount() > 0){
                        cursor.moveToFirst();
                        addItens(cursor.getInt(cursor.getColumnIndex("_id")));
                    }
                    codBarras.setText("");
                    codBarras.requestFocus();
                }
                return false;
            }
        });

Even with requestFocus the edittext focus changes.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.