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!
问题:
回答1:
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.
回答2:
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.