用户输入的Andr​​oid单个字符自动读取 [英] Automatically reading in a single character from user input Android

查看:97
本文介绍了用户输入的Andr​​oid单个字符自动读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到一个方法来做到这一点:我想从键盘用户的单个字符,尽快为他们进入其存储为用户输入的一部分。我并不需要一个文本字段或提交按钮。提示用户从键盘输入一个字符,并将其存储为char或字符串。

I can't seem to find a way to do this: I want to store a single character from a user from the keyboard as part of the user input, as soon as they enter it. I don't need a text field or submit button. Prompt the user to type a character from the keyboard and store it as a char or string.

寻求帮助的任何链接?

推荐答案

赶超Android上的键盘事件没有的EditText是不好的做法,现有的解决方案无法在某些设备上工作,考虑建立你的键盘小部件

Catching keyboard events on android without a edittext is bad practice, existing solutions will not work on some devices, consider creating your keyboard widget

http://developer.android.com/reference/android/inputmethodservice/Keyboard.html

    KeyboardView mKeyboardView;  
    Keyboard mNumberKeyboard; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        mNumberKeyboard = new Keyboard(this, R.xml.keyboard);  
        mKeyboardView=(KeyboardView) this.findViewById(R.id.num_keyboard);
        mKeyboardView.setKeyboard(mNumberKeyboard);
        mKeyboardView.setVisibility(View.VISIBLE); 
        mKeyboardView.setPreviewEnabled(false);
        mKeyboardView.setOnKeyboardActionListener(new OnKeyboardActionListener(){

            @Override
            public void onKey(int arg0, int[] arg1) {
                     SharedPreferences sp = getSharedPreferences("MySharedPreferences", 0);
 SharedPreferences.Editor edit = sp.edit();
 edit.putInt("key", arg0);
edit.commit();
            }

            @Override public void onPress(int primaryCode) {}
            @Override public void onRelease(int primaryCode) {}
            @Override public void onText(CharSequence text) {}
            @Override public void swipeDown() {}
            @Override public void swipeLeft() {}
            @Override public void swipeRight() {}
            @Override public void swipeUp() {}

        });

    }

这篇关于用户输入的Andr​​oid单个字符自动读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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