在EditText中阻止物理键盘输入 [英] Block physical keyboard input in EditText

查看:138
本文介绍了在EditText中阻止物理键盘输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText,该文本不应允许用户通过键盘(软键盘或硬键盘)输入任何内容.此EditText仅应允许用户通过应用程序在屏幕上显示的键(按钮)输入某些内容. 我已经禁用了软键盘,但是找不到禁用硬件键盘输入的方法.可以使用配置为允许通过硬件键盘输入的仿真器来完成通过硬件键盘的输入. 因此,我的问题是,如何在EditText中通过物理键盘阻止输入? 谢谢!

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!

推荐答案

终于明白了!
代码:

Finally got it!
Code:

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

说明:
返回true将告诉侦听器我已经处理了硬件键盘输入.基于Android文档( http://developer.android.com/reference/android/view/View.OnKeyListener.html )

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:将硬件按键事件调度到此视图时要调用的回调的接口定义.回调将在将键事件提供给视图之前被调用.这仅对硬件键盘有用.软件输入法没有触发此侦听器的义务.

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(查看v,int keyCode,KeyEvent事件) 在将硬件键分配给视图时调用.

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

这篇关于在EditText中阻止物理键盘输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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