Validate EditText
<p>I've stucked with the following problem:
I need to validate, that EditText's value is an Integer between 1 and 1000. If it's not - I should put some of the default value into the EditText.
When I try to do such a thing with following code - I get to the infinite loop (well, that was predictable). I've checked the similar questions, but I still can't figure out, how to make the code working.<BR>Are there any other ways to implement the desirable behaviour? How I should edit my code to achieve this?<BR></p>

<pre><code>    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&lt;1 || mailIntervalValue&gt;1000){
                if (mailIntervalValue&lt;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){}
    });
</code></pre>
