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

泪湿孤枕 提交于 2019-12-02 19:07:36

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.

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)?

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!