TextWatcher 出现 java.lang.StackOverflowError 的解决问题
使用TextWatcher 的时候,
class MonthlyQuotaTextWatcher implements TextWatcher{
??@Override
??public void afterTextChanged(Editable s) {
???String str = quotaEditText.getText().toString().trim();
???int length = str.length();
???if(length > 1){
????String st = str.substring(0, 1);
????if( length > 1 && st.equals("0") ){
?????quotaEditText.setText("0");
????}
???}
??}
??@Override
??public void beforeTextChanged(CharSequence s, int start, int count,
????int after) {
??}
??@Override
??public void onTextChanged(CharSequence s, int start, int before,
????int count) {
???
??}
?
??
?}
?
其中??quotaEditText.setText("0");
会出现 java.lang.StackOverflowError 的问题。因为quotaEditText.setText("0");
本身就会回调监听器,】
解决办法:在回调方法afterTextChanged里面加判断条件,例如if(length > 1) if(length > 1) 当满足条件的时候在quotaEditText.setText("0");