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

后端 未结 5 1208
不知归路
不知归路 2021-02-01 18:59

For an EditText box, the user should only be entering valid numbers, so I am using android:inputType=\"numberDecimal\". Unfortunately, the soft keyboar

5条回答
  •  粉色の甜心
    2021-02-01 19:23

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

提交回复
热议问题