Android从键盘读取 [英] Android read from keyboard

查看:77
本文介绍了Android从键盘读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个登录屏幕,根本没有文本字段.当用户使用rfid扫描仪扫描其id令牌时,我将得到一个8个字符长的字符串.它与使用键盘的原理相同(只是速度更快).我希望我的登录活动在用户扫描其令牌而不是之前进行操作.是否有实现此目的的明智方法?我没有任何按钮或文本字段,因此必须阅读键盘输入,而不必听EditText字段.

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.

因此,一个带有图像(锁)的空白屏幕将侦听rfid阅读器(或键盘),并在字符数为8时起作用.

So, an empty screen with an image(lock), that listens to an rfid reader(or keyboard) and act when the character count is 8.

推荐答案

如果在Activity中实现了onKeyDown(),并且没有其他用于处理按键事件的UI小部件,则应该从键盘上获得每次按键操作.下面的示例至少应适用于A-Z,旨在将按键简单地打印"到TextView.

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);
    }
}

这篇关于Android从键盘读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆