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.