禁止在 EditText 焦点上显示软键盘

发布于 2024-10-23 19:23:25 字数 745 浏览 1 评论 0原文

我试图定义一组按钮,允许用户将数据输入到 EditText 框中。我想要 EditText 框的所有默认功能,除了软键盘的弹出。唯一允许输入到 EditText 框中的数据应该是我定义的按钮中的数据。我试图通过捕获触摸事件并返回 true 来抑制软键盘。 (根据在此线程上找到的对话

private OnTouchListener txtTouchListener = new OnTouchListener()
    {
    @Override
    public boolean onTouch(View v, MotionEvent event)
        {
        //Return true to suppress the passing of the event on to the OS
        return true;
        }
    };

问题在于然后,此方法会阻止长按事件的触发。为了解决这个问题,我可以返回 false,然后处理长按事件。然而,这会使短按一下调出软键盘。此外,长按时不仅软键盘会被抑制,上下文菜单也会被抑制。我正在寻找一种方法来阻止键盘在短(或长)单击时出现,但保留所有其他功能(短单击时更新光标位置,长单击时显示 EditText 上下文菜单等)

对此有任何想法非常感谢!

I am trying to define a set of buttons that allow the user to enter data into an EditText box. I want all the default functionality of an EditText box except for the pop up of the soft keyboard. the only data to be allowed to enter into the EditText box should be the data from the buttons I have defined. I am trying to suppress the soft keyboard by catching the touch event and returning true. (per the conversation found on this thread)

private OnTouchListener txtTouchListener = new OnTouchListener()
    {
    @Override
    public boolean onTouch(View v, MotionEvent event)
        {
        //Return true to suppress the passing of the event on to the OS
        return true;
        }
    };

The problem is that this method then blocks the long click event from firing. To solve this I can return false and then handle the long click event. However, this then makes the short click bring up the soft keyboard. Also, upon long click not only is the soft keyboard suppressed, but so is the context menu. I am looking for a way to stop the keyboard from appearing on a short (or long) click but keep all other functionality (updating the cursor position on short click, on long click show the EditText context menu, etc.)

Any ideas on this is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2024-10-30 19:23:25

线程建议:

EditText.setInputType(InputType.TYPE_NULL);

或者将 XML 属性中的 inputType 设置为 none

不过我自己没试过。

This thread suggests:

EditText.setInputType(InputType.TYPE_NULL);

Or you set the inputType in the XML Attribute to none.

Not tried it myself though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文