views:

41

answers:

1

I'd like to allow possible inputs of 0-9, "," or "-" for EditText and couldn't find an answer.

I know using the "|" as an separator is possible:

android:inputType="number|text"

But how can I archive something like this (too bad it doesn't work):

android:inputType="number|,|-"

Does anyone know?

Regards,

cody


Addition to the comment below:

private class MyKeylistener extends NumberKeyListener {

    public int getInputType() {
      return InputType.TYPE_CLASS_NUMBER;
    }
    @Override
    protected char[] getAcceptedChars() {
      return new char[] {'0','1','2','3','4','5','6','7','8','9',',','-'};
    }

}

+1  A: 

I don't think there is a way to provide an arbitrary filter like that. You have to use the provided filter types.

If you can't find a combination of those types that does what you want, you probably need to do the filtering yourself using a TextWatcher or InputFilter on the EditText.

Mayra
Thanks for the quick response. You put the idea in my mind to search for a KeyListener. Here's what I found, perhaps it's useful for someone:
cody
Just a note (I think the end of your comment got lost, so not sure exactly what you were saying, but...) The OnKeyListener interface will only notify you of text entered via a physical keyboard, not from the onscreen keyboard (ime). To listen for text changes from both you need to use one of methods I mentioned.
Mayra
Ok.. good your mention that - in the emulator I couldn't type other characters into the field. I didn't know how to post code in the comment, I posted it above. Best Regards, cody
cody