1

So i wan't to have an editText that will do some stuff automaticly.

The first thing is that it should only allow numbers as insert.

The second and in my opinion harder think is that when user insert for example 5 numbers in adds some sign. For example i write:

    12345

and then program should add a sign

    12345-

And now i can type more.

    12345-6789

I was googling for predifine text in EditText but i found nothing useful...

gabrjan
  • 3,080
  • 9
  • 40
  • 69
  • 1
    You want an input filter. See [Masked Input Using EditText Widget in Android][1] [1]: http://stackoverflow.com/questions/2912375/masked-input-using-edittext-widget-in-android – Philip Sheard Sep 11 '12 at 13:21
  • Philip that's what i was looking for, i hope i manage to do it now... – gabrjan Sep 11 '12 at 13:37

3 Answers3

2

For 1) add android:inputType="number" in xml

For 2) You can have two different edittexts separated by - and android:maxLength="5" in xml and later join the values

Though I am sure there must be some better way to do (2)

Sunny Kumar Aditya
  • 2,806
  • 4
  • 26
  • 38
  • Tnx for the idea on (2), i found (1) for myself, i'm steal looking for (2) but nothing smart came up... – gabrjan Sep 11 '12 at 12:53
0

For 2) couldn't you check onKeyDown (int keyCode, KeyEvent event) ?

So when the user presses a button you use this function to calculate the length of your edittext and if the length equals 5, you insert the character you want.

Just as a note, I haven't tried inserting a "-" sign in an edittext when you define it to only be numbers.

Zyber
  • 1,034
  • 8
  • 19
  • well i guess i could, but that is a field in a form, so it's hard to know in which editText is user typing ... – gabrjan Sep 11 '12 at 13:03
  • check this [link](http://stackoverflow.com/questions/4551757/allow-only-number-and-period-in-edit-text-in-android) especially the answer by Chiara for formating the text. Or this [link](http://stackoverflow.com/questions/5064576/automatically-format-phone-number-in-edittext), maybe they can help you – Zyber Sep 11 '12 at 13:11
0

So i find a solution tenx to Philip coment. U have to use filter to do so. My code is now this:

   InputFilter filter = new InputFilter() {

        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dend) {

            if(end==0){
                if(is_minus){jis_minus=false;}
                else number--;
            }
            for (int i = start; i < end; i++) { 

                if (!Character.isDigit(source.charAt(i))) { 
                    return "";
                } 
                else{
                    number++;
                }
                if(number==5)
                    {
                    is_minus=true;
                    return (source.charAt(i)+"-");
                    }

        } 
        return null; 
        }
};

After that u just apend filter to the textbox u want...

gabrjan
  • 3,080
  • 9
  • 40
  • 69