Block physical keyboard input in EditText

2020-07-24 05:09发布

I have a EditText that should not allow the user to input anything through the keyboard (soft or hard). This EditText should only allow the user to input something through keys(buttons) displayed in the screen by the app. I have disabled the soft keyboard, but I can't find a way to disable the hardware keyboard input. This input via hardware keyboard can be done using a emulator that is configured to allow input through the hardware keyboard. So, my question is, How can I block the input via physical keyboard in a EditText? Thank you!

2条回答
劳资没心,怎么记你
2楼-- · 2020-07-24 05:23

Finally got it!
Code:

editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            return true;
        }
    });

Explanation:
Returning true will tell the listener that I've already handled the hardware keyboard input. Based on Android documentation (http://developer.android.com/reference/android/view/View.OnKeyListener.html)

View.OnKeyListener: Interface definition for a callback to be invoked when a hardware key event is dispatched to this view. The callback will be invoked before the key event is given to the view. This is only useful for hardware keyboards; a software input method has no obligation to trigger this listener.

onKey(View v, int keyCode, KeyEvent event) Called when a hardware key is dispatched to a view.

查看更多
男人必须洒脱
3楼-- · 2020-07-24 05:29

You could try these according to what you want in your xml layout

android:longClickable="false"
android:clickable="false"    
android:cursorVisible="false"
android:editable="false"

Then make the edittext show what the user typed through the keyboard you made in the app.

查看更多
登录 后发表回答