Skip to content Skip to sidebar Skip to footer

Android : Keylistener Of Edittext Not Working

I'm trying to filter the input char of an EditText to allow only valid float value. (e.g. :'12,45', only one comma, ...). For this, I use the KeyListener (dont confuse with onKeyL

Solution 1:

Looking at the Android docs they state in relation to KeyListener that:

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

And for the method getInputType():

If you return TYPE_NULL then no soft keyboard will provided. In other words, you must be providing your own key pad for on-screen input and the key listener will be used to handle input from a hard keyboard.

If you return any other value, a soft input method will be created when the user puts focus in the editor, which will provide a keypad and also consume hard key events. This means that the key listener will generally not be used, instead the soft input method will take care of managing key input as per the content type returned here.

So because you return a content type the KeyListener is ignored. Looks like this isn't possible then or did you find a work around?

Perhaps you could create a custom TextWatcher instead and then set this on the EditText using EditText.addTextChangedListener(TextWatcher textWatcher).?

Post a Comment for "Android : Keylistener Of Edittext Not Working"