java - setKeyListener 将覆盖 setInputType 并更改键盘

标签 java android android-edittext searchview android-inputtype

大家好,我在两台设备之间遇到了问题。

在使用 InputTypeKeyListener 的实践中,我正在操作一个 EditText 以使其从数字键盘接收逗号和数字(有关更多背景信息,请查看我之前的 question )。

使用下面的代码我可以做类似的事情:

    editText.setInputType(InputType.TYPE_CLASS_NUMBER + InputType.TYPE_NUMBER_FLAG_DECIMAL);
    editText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));

enter image description here

不幸的是,这不适用于某些设备,例如 Asus ZenPad 8.0 或某些 Samsung。

仅显示数字键盘而没有逗号,因此用户无法单击和使用它,重要的一点是注释下面的代码行可以正确显示带逗号的数字键盘。但通过这样做,他们将无法再在同一搜索中使用多个逗号。

    editText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));

没有 KeyListener

使用 KeyListener

我还阅读了有关此的 android 文档,它说 (source):

Interface for converting text key events into edit operations on an Editable class. Note that for most cases this interface has been superceded by general soft input methods as defined by InputMethod; it should only be used for cases where an application has its own on-screen keypad and also wants to process hard keyboard events to match it.

Key presses on soft input methods are not required to trigger the methods in this listener, and are in fact discouraged to do so. The default android keyboard will not trigger these for any key to any application targetting Jelly Bean or later, and will only deliver it for some key presses to applications targetting Ice Cream Sandwich or earlier.

所以基本上对于旧版本的 android 这不起作用?我怎样才能复制上述行为? (基本上我怎样才能使用无限数量的逗号的数字键盘)

有关更多详细信息,请发表评论,我很乐意回复并添加更多信息!

提前致谢, 贾科莫。

最佳答案

这完全取决于您使用 EditText 的目的(如果字母也可见会很悲惨吗?)您可以简单地使用默认输入类型并简单地对其设置一个过滤器,以便它只接受数字和一个“字符”(即逗号)。这是一个link让你这样做。诚然,它看起来不会那么漂亮,但它会为您的用户提供适用于所有设备的可靠体验。

另一种方法是如上所述创建自己的键盘

关于java - setKeyListener 将覆盖 setInputType 并更改键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61212695/

相关文章:

安卓最近应用检测

android - EditText聚焦drawable点击

java - https 与 ssl 固定

java - 按 OK 按钮后 JFace 对话框保持打开状态

java - 如何在静态初始化之前做一些事情

java - 从流读取时出现 IndexOutOfBoundsException

android - Android NDK错误

java - 从 Canvas 中绘制的弧中获取坐标

android - 在android中的edittext中使用spannable

android - 如何在 editText 中删除这个空格?