android - Validate EditText -


i've stucked following problem: need validate, edittext's value integer between 1 , 1000. if it's not - should put of default value edittext. when try such thing following code - infinite loop (well, predictable). i've checked similar questions, still can't figure out, how make code working.
are there other ways implement desirable behaviour? how should edit code achieve this?

    long mailintervalvalue;     edittext etmailinterval;         .         .         .         .     etmailinterval=(edittext)findviewbyid(r.id.et_mail_check_interval);     etmailinterval.settext(mailintervalvalue.tostring());     etmailinterval.addtextchangedlistener(new textwatcher(){         public void aftertextchanged(editable s) {             integer t=integer.getinteger(s.tostring());             if (t==null){                 s.clear();                 s.append(mailintervalvalue.tostring());                 mailintervalvalue=messagemanager.default_time;             }             else                 mailintervalvalue=t.longvalue();             if (mailintervalvalue<1 || mailintervalvalue>1000){                 if (mailintervalvalue<1)                     mailintervalvalue=1l;                 else                     mailintervalvalue=1000l;                 s.clear();                 s.append(mailintervalvalue.tostring());                 toast.maketext(mainactivity.this, mainactivity.this.getstring(r.string.settings_timer_fail),                         toast.length_short).show();             }              savemailerprefs();         }         public void beforetextchanged(charsequence s, int start, int count, int after){}         public void ontextchanged(charsequence s, int start, int before, int count){}     }); 

i suggest using intended tool job: input filter might better alternative.

here example code , documentation. if need more complex, recommend looking @ this code.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -