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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2830664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:32:29  来源:igfitidea点击:

How do I use InputType=numberDecimal with the "phone" soft keypad?

androidnumeric-keypad

提问by Adam Dunn

For an EditTextbox, 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 numberDecimalonly 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

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

I have attempted to use android:inputType="numberDecimal|phone", but the numberDecimalaspect 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 DigitsKeyListenerin 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_PHONEset by the xml layout).

我曾尝试使用android:inputType="numberDecimal|phone",但numberDecimal似乎忽略了位标志的方面。我也尝试过android:inputType="phone"与 结合使用android:digits="0123456789-.",但它仍然允许多个负号或小数点(inputType="number"对于类似的事情有非常好的错误检查,并且不会让用户输入)。我也尝试过android:inputType="phone"在 xml 布局文件中使用,而DigitsKeyListener在 java 代码中使用 a ,但是那只是使用默认数字软键盘(只有顶行有数字的那个)(它似乎是 set InputType.TYPE_CLASS_NUMBER,这使InputType.TYPE_CLASS_PHONEset无效通过 xml 布局)。

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

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

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

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

采纳答案by Adam Dunn

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:

到目前为止,我决定做的是扩展 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;
    }
}

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

这样做有什么问题吗(将 getInputType() 的返回切换到超类不想要的东西)?

回答by tjkn24

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.

希望这对你有帮助。

回答by joe1327

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

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;
    }
}

then you have to instance like this

那么你必须像这样实例

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

回答by joe1327

You just use the phone keyboard and check the input by yourself. It isn't a very big condition to test if the input is a valid digit between 0 and 9.

您只需使用电话键盘并自行检查输入。测试输入是否为 0 到 9 之间的有效数字并不是一个很大的条件。

回答by codinguser

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

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