Keep focus on edittext after pressing Enter

1

I have a barcode reader that does the action of placing text in edittext and pressing enter automatically, implemented the function while reading but 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 the requestFocus the focus of edittext changes.

    
asked by anonymous 20.04.2018 / 16:12

0 answers