2014-06-13 2 views
1

"q"가 edittext의 마지막 문자로 기록 될 때마다 TextWatcher를 첨부하고 "q"는 "a"로 바뀝니다. 나는 다음을 사용한다 :TextWatcher - 문자열의 마지막 문자를 대체합니다.

public void afterTextChanged(Editable s) { 
     // TODO Auto-generated method stub 
     if(s.length() > 0 && s.toString().charAt(s.length()-1) == 'q') 
     { 
      current_string = s.toString(); 
      current_string = current_string.substring(0, (current_string.length()-1)); 
      et.setText(current_string); 
      } 
     } 

그러나 코드를 테스트 할 때 "q"를 입력해도 아무런 변화가 없다. 도움? 고마워요

+0

당신의 문자열로 문자 a를 추가하는 것을 잊었다()' –

+0

나는 창피 D합니까 방법 : –

+0

당신을 'StringBuilder'를 사용해야합니다. 빠른 방법은'et.setText (current_string + "a");' –

답변

0

afterTextChanged을 사용하면 반복적으로 호출되어 무한 루프에 빠질 수 있으므로 위험합니다. 문서로

은 말하고있다 :

 It is legitimate to make further changes to s from this callback, 
    but be careful not to get yourself into an infinite loop, because any 
    changes you make will cause this method to be called again recursively 

대신 onTextChanged를 사용합니다.

문제에

당신은 당신이 문자열`후`A`를 추가하는 것을 잊었다

public void onTextChanged (CharSequence s, int start, int before, int count) { 
    // TODO Auto-generated method stub 
    if(s.length() > 0 && s.toString().charAt(s.length()-1) == 'q') 
    { 
     current_string = s.toString(); 
     current_string = current_string.substring(0, (current_string.length()-1)); 
     et.setText(current_string + "a"); //add the a after q is deleted 
     } 
    } 
}