다음과 같은 문제가 있습니다. 유효성을 검사해야하는데 EditText의 값은 1에서 1000 사이의 정수입니다. 그렇지 않은 경우 EditText에 기본값을 넣어야합니다. 다음 코드로 이러한 작업을하려고 할 때 - 무한 루프가 발생합니다 (예상 할 수 있음). 비슷한 질문을 했는데도 코드를 작동시키는 방법을 알아 내지 못합니다.
바람직한 동작을 구현하는 다른 방법이 있습니까? 어떻게 이것을 달성하기 위해 코드를 편집해야합니까?
Validate EditText
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){}
});
고마워, 내가 필요한 것 같아. – Dirol
환영합니다. 희망이 당신을 위해 작동합니다. –