如何使用InputType = numberDecimal与"电话"软键盘? [英] How do I use InputType=numberDecimal with the "phone" soft keypad?

查看:157
本文介绍了如何使用InputType = numberDecimal与"电话"软键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于的EditText 中,用户只能输入有效的数字,所以我使用安卓inputType =numberDecimal。不幸的是,软键盘的Andr​​oid带来了拥有数只沿顶行,而接下来的三个行具有其他各种符号(美元符号,百分号,感叹号,空间等)。因为 numberDecimal 只接受数字0-9,负号和小数点,那就更有意义,在一个3x3使用手机软键盘(0-9格,加上一些其它符号)。这将使按钮更大,更容易击中(因为它是一个4×4网格,而不是一个10x4网格在同一画面区域)。不幸的是,使用安卓inputType =手机允许非数字字符,如括号

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

我试图使用安卓inputType =numberDecimal |手机,但其中的 numberDecimal 纵横比特标志似乎被忽略。我也尝试过使用安卓inputType =手机联合安卓位数=0123456789 - ,但仍然允许多个负号或小数点( inputType =号有真正好的错误检查这样的事情,而且不会让用户即使键入在)。我也尝试过使用安卓:在XML布局文件inputType =手机,而使用 DigitsKeyListener 的java的code,但只是使用默认值软键盘(一个具有数字只能沿着上排)(它似乎设置 InputType.TYPE_CLASS_NUMBER ,其中空洞的 InputType.TYPE_CLASS_PHONE 的XML布局设置)。

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?

推荐答案

到目前为止,我已经决定做的是延长DigitsKeyListener并覆盖getInputType(),这样它会返回InputType.TYPE_CLASS_PHONE。这使我使用方便的过滤器()在DigitsKeyListener,但在同一时间使用TYPE_CLASS_PHONE软键盘。我是新来的Andr​​oid编程,但是这似乎没有打破任何工作。 code是这样的:

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

这有什么错在做这个(交换getInputType()返回的东西,超不打算)?

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

这篇关于如何使用InputType = numberDecimal与"电话"软键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆