我是Android的新手,我的代码有一些问题。
我想为大学期末考试创建便携式数据终端,
问题
我不知道如何使Android Studio中的EditText始终保持焦点,我的意思是,当我使用扩展扫描仪扫描条形码时,扫描仪向应用程序中的EditText发送一些条形码,该扫描仪带有回车键,在按下Enter键并单击/单击数据后,数据移到了数据库中,而EditText仍聚焦于下一次扫描,但实际上,在Enterkey单击后,EditText失去了焦点
我尝试使用EditText.requestFocus()
,但它不起作用,
代码
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;
}
});
按下Enter键后,我需要再次重新聚焦PluCode。