问题
I have this logon screen with no textfields at all. When the user scans his id token with rfid scanner, i'll get a 8 character long string. Its the same princip as using an keyboard(just faster). I want my login activity to act, when the user has scanned his token and not before. Is there a smart way implementing this? I can't have any button or textfields, so I have to read keyboard input without listening to a EditText field.
So, an empty screen with an image(lock), that listens to an rfid reader(or keyboard) and act when the character count is 8.
回答1:
If you implement onKeyDown()
in your Activity
and there is no other UI widget that handles key events you should get every key press from your keyboard. Below example should work for A-Z at least and is intended to simply "print" the keypresses to a TextView
.
You might need to add a more sophisticated way to map keycode to character if that is not enough. (e.g. space does not work, numbers probably neither)
public class KeyActivity extends Activity {
// should work for a-z
private static final Pattern KEYCODE_PATTERN = Pattern.compile("KEYCODE_(\\w)");
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// there is just a TextView, nothing that handle keys
mTextView = (TextView) findViewById(R.id.textView1);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
String key = KeyEvent.keyCodeToString(keyCode);
// key will be something like "KEYCODE_A" - extract the "A"
// use pattern to convert int keycode to some character
Matcher matcher = KEYCODE_PATTERN.matcher(key);
if (matcher.matches()) {
// append character to textview
mTextView.append(matcher.group(1));
}
// let the default implementation handle the event
return super.onKeyDown(keyCode, event);
}
}
来源:https://stackoverflow.com/questions/12038440/android-read-from-keyboard