问题
Hi everyone I'm having a problem between two devices.
In practice using the InputType
and the KeyListener
I'm manipulating an EditText
to make it receive the commas and numbers from the numeric pad (for more background check my previous question).
Using the following code I'm able to do something like that:
editText.setInputType(InputType.TYPE_CLASS_NUMBER + InputType.TYPE_NUMBER_FLAG_DECIMAL);
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));
Unfortunately this doesn't work on some devices like Asus ZenPad 8.0
or some Samsung.
Only the numeric pad is shown without the comma and therefore the user is unable to click and use it, an important note is that commenting the line of code below shows me correctly the pad with the comma available. But by doing this they are no longer able to use multiple commas in the same search.
editText.setKeyListener(DigitsKeyListener.getInstance("0123456789,"));
Without the KeyListener
With the KeyListener
I've also read the android documentation about this and it says (source):
Interface for converting text key events into edit operations on an Editable class. Note that for most cases this interface has been superceded by general soft input methods as defined by InputMethod; it should only be used for cases where an application has its own on-screen keypad and also wants to process hard keyboard events to match it.
Key presses on soft input methods are not required to trigger the methods in this listener, and are in fact discouraged to do so. The default android keyboard will not trigger these for any key to any application targetting Jelly Bean or later, and will only deliver it for some key presses to applications targetting Ice Cream Sandwich or earlier.
So basically for old versions of android this doesn't work? How can I replicate the above behavior? (Basically how can I use the numeric keyboard with an unlimited number of comma)
For any more details just comment i will gladly reply and add more info!
Thanks in advance, Giacomo.
回答1:
it all depends on what you are using the EditText for (would it be tragic if letters were also visible?) you could simply use default input type and simply set a filter on it so that it only accepts numbers and the one "character" (i.e. the comma) when you hit the keys. here is a link that lets you do that. admittedly it wont look as pretty but it will provide your users with a reliable experience that will work on all devices.
the alternative is to create your own keyboard as mentioned above
来源:https://stackoverflow.com/questions/61212695/setkeylistener-will-override-setinputtype-and-change-the-keyboard