在EditText中阻止物理键盘输入

在EditText中阻止物理键盘输入

本文介绍了在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)

onKey(查看v,int keyCode,KeyEvent事件) 在将硬件键分配给视图时调用.

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

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

08-16 05:50