2017-03-29 75 views
0

안드로이드에 문자를 입력하려면 변경하고 싶습니다. 사용자 유형 space 또는 - 그것이 _안드로이드의 TextWatcher 경고 및 슬로우 타입

로 변경됩니다 그래서 시도 :

TextWatcher tt = null; 

final EditText etUsername = (EditText) findViewById(R.id.etUsername); 
    tt = new TextWatcher() { 
     public void afterTextChanged(Editable s){ 
      etUsername.setSelection(s.length()); 
     } 
     public void beforeTextChanged(CharSequence s,int start,int count, int after){} 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      etUsername.removeTextChangedListener(tt); 
      etUsername.setText(etUsername.getText().toString().replace(" ", "_")); 
      etUsername.setText(etUsername.getText().toString().replace("-", "_")); 
      etUsername.addTextChangedListener(tt); 
     } 
    }; 
    etUsername.addTextChangedListener(tt); 

을 "작품"하지만 사용자 유형 빠른 일부 문자가 나타나지 않습니다와 나는 몇 가지 경고를 가지고있는 경우 :

W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: commitText on inactive InputConnection 
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection 
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: commitText on inactive InputConnection 
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection 
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: commitText on inactive InputConnection 
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection 
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: commitText on inactive InputConnection 
W/IInputConnectionWrapper: endBatchEdit on inactive InputConnection 
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 
W/IInputConnectionWrapper: requestCursorAnchorInfo on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection 
W/IInputConnectionWrapper: finishComposingText on inactive InputConnection 

어떤 아이디어가 잘못 되었나요? 그럼 그냥 교체 ""또는 "-"

답변

1

을 제거하고 텍스트 변경에 또 다시 텍스트 변경 리스너를 추가 할 필요가 시도하지, 당신의 편집이가 있는지 확인하기 위해 조건을 넣어 EditText로 설정하십시오. 안드로이드 개발자에서 사용할 수

etUsername.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       if (s.length() > 0 && (s.toString().contains("-") || s.toString().contains(" "))) { 
        etUsername.setText(s.toString().replace("-", "_").replace(" ", "_")); 
       } 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       etUsername.setSelection(s.length()); 
      } 
     }); 
+0

이 굉장하다, 대단히 감사합니다. –

1

이 Textwatcher

TextWatcher watch = new TextWatcher(){ 

    @Override 
    public void afterTextChanged(Editable arg0) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
      int arg3) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onTextChanged(CharSequence s, int a, int b, int c) { 
     // TODO Auto-generated method stub 

     output.setText(s); 
     if(a == 9){ 
      Toast.makeText(getApplicationContext(), "Maximum Limit Reached", Toast.LENGTH_SHORT).show(); 
     } 
    }}; 
+0

더 가이드 - [링크] (https://developer.android.com/reference/android/text/TextWatcher.html) – ITSGuru