Skip to content Skip to sidebar Skip to footer

Automatically Reading In A Single Character From User Input Android

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 fie

Solution 1:

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; 

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



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

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

            @OverridepublicvoidonPress(int primaryCode) {}
            @OverridepublicvoidonRelease(int primaryCode) {}
            @OverridepublicvoidonText(CharSequence text) {}
            @OverridepublicvoidswipeDown() {}
            @OverridepublicvoidswipeLeft() {}
            @OverridepublicvoidswipeRight() {}
            @OverridepublicvoidswipeUp() {}

        });

    }

Post a Comment for "Automatically Reading In A Single Character From User Input Android"