0

I'm newbie in Android and I have some problems with my code.

I want to create a Portable Data Terminal for my final exam in college,

Problem,

I don't know how to make the EditText in Android Studio always focus, I mean, when I scan the barcode using an extended scanner, the scanner sending some barcode to EditText in the apps, the scanner comes with enter key, and after enterkey pressed/clicked the data move to database and The EditText still focus for next scan, but in the real after enterkey clicked the EditText lost the focus

I've tried using EditText.requestFocus() but it doesn't work,

Code

PluCode.setOnKeyListener(new View.OnKeyListener() {  

  @Override

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && (keyCode == 
KeyEvent.KEYCODE_ENTER)){

               DBAdapter dbAdapter = new 
DBAdapter(getApplicationContext(),null,null,1);
                String kodetoko = StoreCode.getSelectedItem().toString();
                String plutoko = PluCode.getText().toString();
                String qtytoko = Qty;
                TransactionModel transactionmodel = new 
TransactionModel(kodetoko,plutoko,qtytoko);

                dbAdapter.onOpen();
                dbAdapter.insertTransaction(transactionmodel);
                Cursor cursor = dbAdapter.showAllData2();
                cursor.moveToFirst();
                TableLayout tablelayouttransaction = 
(TableLayout)findViewById(R.id.transactionTableLayout);
                int childCount = tablelayouttransaction.getChildCount();

                // refresh row
                if (childCount > 0) {
                    tablelayouttransaction.removeViews(0, childCount);
                    int countrow = tablelayouttransaction.getChildCount();
                    if(countrow == 0){
                        do {
                            TableRow row = (TableRow) 
getLayoutInflater().inflate(R.layout.layout_row_transaction, null);
                            ((TextView) 
row.findViewById(R.id.idkodetoko)).setText(cursor.getString(0));
                            ((TextView) 
row.findViewById(R.id.idplu)).setText(cursor.getString(1));
                            ((TextView) 
row.findViewById(R.id.idqty)).setText(cursor.getString(2));
                            tablelayouttransaction.addView(row);

                        } while (cursor.moveToNext());

                    }
                }else{
                    do {
                        TableRow row = (TableRow) 
getLayoutInflater().inflate(R.layout.layout_row_transaction, null);
                        ((TextView) 
row.findViewById(R.id.idkodetoko)).setText(cursor.getString(0));
                        ((TextView) 
row.findViewById(R.id.idplu)).setText(cursor.getString(1));
                        ((TextView) 
row.findViewById(R.id.idqty)).setText(cursor.getString(2));
                        tablelayouttransaction.addView(row);

                    } while (cursor.moveToNext());

                }

                dbAdapter.close();
                PluCode.getText().clear();
                PluCode.requestFocus();
                displayToast("Data saved");

                return true;
            }
            return false;
        }
    });

I need PluCode refocus again after enterkey pressed.

Community
  • 1
  • 1
Maverick
  • 1
  • 2
  • Possible duplicate of [Stop EditText from gaining focus at Activity startup](https://stackoverflow.com/questions/1555109/stop-edittext-from-gaining-focus-at-activity-startup) – CootMoon Feb 18 '19 at 18:11
  • Try to add `PluCode.requestFocus();` before your last `return true;`. I think that your EditText lost focus with the `KeyEvent.KEYCODE_ENTER` and `KeyEvent.ACTION_UP`. Regards – Artificioo Feb 18 '19 at 18:28
  • 1
    okay i will try it, and i report soon, thanks Artificioo – Maverick Feb 18 '19 at 18:33
  • still lost the focus @Artificioo – Maverick Feb 18 '19 at 18:36
  • Mmm, okay, try changing ACTION_DOWN to ACTION_UP and only return false in this case. In other actions/keys, return true – Artificioo Feb 18 '19 at 18:40
  • 1
    @Artificioo thank you so much it's work :D, i change the ACTION_DOWN to ACTION_UP – Maverick Feb 19 '19 at 14:49

0 Answers0