如何将 InputType=numberDecimal 与“电话”一起使用软键盘?

发布于 2024-09-01 15:32:26 字数 969 浏览 3 评论 0原文

对于 EditText 框,用户应该只输入有效的数字,因此我使用 android:inputType="numberDecimal"。不幸的是,Android 带来的软键盘仅在顶行有数字,而接下来的三行有各种其他符号(美元符号、百分号、感叹号、空格等)。由于 numberDecimal 只接受数字 0-9、负号和小数点,因此使用“电话”软键盘(3x3 网格中的 0-9,加上一些其他符号)会更有意义)。这将使按钮更大并且更容易点击(因为它是同一屏幕区域中的 4x4 网格而不是 10x4 网格)。不幸的是,使用 android:inputType="phone" 允许非数字字符,例如括号

我尝试使用 android:inputType="numberDecimal|phone",但是 <位标志的 code>numberDecimal 方面似乎被忽略。我还尝试过将 android:inputType="phone"android:digits="0123456789-." 结合使用,但这仍然允许多个负号或小数点( inputType="number" 对类似的事情有非常好的错误检查,并且不会让用户输入它)。我还尝试在 xml 布局文件中使用 android:inputType="phone" ,同时在 java 代码中使用 DigitsKeyListener ,但随后只使用默认的数字软键盘(仅在顶行有数字的键盘)(它似乎设置了 InputType.TYPE_CLASS_NUMBER,这使得 xml 布局设置的 InputType.TYPE_CLASS_PHONE 无效)。

编写自定义 IME 是行不通的,因为用户必须选择 IME 作为应用程序外部的全局选项。

有什么方法可以使用“电话”式软键盘,同时对输入内容使用“数字”限制?

For an EditText box, the user should only be entering valid numbers, so I am using android:inputType="numberDecimal". Unfortunately, the soft keyboard that Android brings up has numbers only along the top row, while the next three rows have various other symbols (dollar sign, percent sign, exclamation mark, space, etc). Since the numberDecimal only accepts numbers 0-9, negative sign, and decimal point, it would make more sense to use the "phone" soft keyboard (0-9 in a 3x3 grid, plus some other symbols). This would make the buttons larger and easier to hit (since it's a 4x4 grid rather than a 10x4 grid in the same screen area). Unfortunately, using android:inputType="phone" allows non-numeric characters such as parentheses

I have attempted to use android:inputType="numberDecimal|phone", but the numberDecimal aspect of the bit flag seems to be ignored. I have also tried using android:inputType="phone" in combination with android:digits="0123456789-.", but that still allows multiple negative signs or decimal points (inputType="number" has really good error checking for things like that, and won't let the user even type it in). I have also tried using android:inputType="phone" in the xml layout file, while using a DigitsKeyListener in the java code, but then that just uses the default number soft keyboard (the one that has numbers only along top row) (it appears to set InputType.TYPE_CLASS_NUMBER, which voids the InputType.TYPE_CLASS_PHONE set by the xml layout).

Writing a custom IME wouldn't work, since the user would have to select the IME as a global option outside the app.

Is there any way to use the "phone" style soft keyboard while also using the "number" restrictions on what is entered?

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

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

发布评论

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

评论(5

飘落散花 2024-09-08 15:32:26

我也有同样的问题。这对我有用:

<item name="android:inputType">numberDecimal</item>
<item name="android:digits">0123456789.</item>

希望这对您有帮助。

I had the same problem. This works for me:

<item name="android:inputType">numberDecimal</item>
<item name="android:digits">0123456789.</item>

Hopefully this helps you.

追我者格杀勿论 2024-09-08 15:32:26

到目前为止,我决定扩展 DigitsKeyListener 并重写 getInputType(),以便它将返回 InputType.TYPE_CLASS_PHONE。这允许我在 DigitsKeyListener 中使用方便的 filter(),但同时使用 TYPE_CLASS_PHONE 软键盘。我是 Android 编程新手,但这似乎可以正常工作,不会破坏任何东西。代码是这样的:

import android.text.method.DigitsKeyListener;
import android.text.InputType;
public class CustomDigitsKeyListener extends DigitsKeyListener
{
    public CustomDigitsKeyListener() {
        super(false, false);
    }

    public CustomDigitsKeyListener(boolean sign, boolean decimal) {
        super(sign, decimal);
    }

    public int getInputType() {
        return InputType.TYPE_CLASS_PHONE;
    }
}

这样做有什么问题吗(将 getInputType() 的返回值切换为超类不希望的值)?

So far, what I've decided to do is extend the DigitsKeyListener and override getInputType() so that it will return InputType.TYPE_CLASS_PHONE. This allows me to use the handy filter() in DigitsKeyListener, but at the same time use the TYPE_CLASS_PHONE soft keyboard. I'm new to Android programming, but this appears to work without breaking anything. Code is something like this:

import android.text.method.DigitsKeyListener;
import android.text.InputType;
public class CustomDigitsKeyListener extends DigitsKeyListener
{
    public CustomDigitsKeyListener() {
        super(false, false);
    }

    public CustomDigitsKeyListener(boolean sign, boolean decimal) {
        super(sign, decimal);
    }

    public int getInputType() {
        return InputType.TYPE_CLASS_PHONE;
    }
}

Is there anything wrong in doing this (switching the return of getInputType() to something that the superclass didn't intend)?

篱下浅笙歌 2024-09-08 15:32:26

Adam Dunn 代码工作完美,但没有展示如何实现该类

import android.text.method.DigitsKeyListener;
import android.text.InputType;
public class CustomDigitsKeyListener extends DigitsKeyListener
{
    public CustomDigitsKeyListener() {
        super(false, false);
    }

    public CustomDigitsKeyListener(boolean sign, boolean decimal) {
        super(sign, decimal);
    }

    public int getInputType() {
        return InputType.TYPE_CLASS_PHONE;
    }
}

,那么你必须像这样实例化

MyTextView.setKeyListener(new CustomDigitsKeyListener(true,true));

Adam Dunn code works perfect, but didnt show how to implement the class

import android.text.method.DigitsKeyListener;
import android.text.InputType;
public class CustomDigitsKeyListener extends DigitsKeyListener
{
    public CustomDigitsKeyListener() {
        super(false, false);
    }

    public CustomDigitsKeyListener(boolean sign, boolean decimal) {
        super(sign, decimal);
    }

    public int getInputType() {
        return InputType.TYPE_CLASS_PHONE;
    }
}

then you have to instance like this

MyTextView.setKeyListener(new CustomDigitsKeyListener(true,true));
抹茶夏天i‖ 2024-09-08 15:32:26
android:inputType="phone"
android:digits="0123456789."

将其添加到您的 EditText xml 布局中。
这将显示手机布局,但只允许用户点击指定的数字。
用户点击其他字符将被忽略。

android:inputType="phone"
android:digits="0123456789."

Add this to your EditText xml layout.
This will show phone layout, but only allow user to tap digits specified.
User tapping other characters will be ignored.

无远思近则忧 2024-09-08 15:32:26

尝试在 EditText 视图中使用 android:numeric="integer"

Try using android:numeric="integer" in your EditText view.

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