2017-03-29 4 views
0

Ich möchte auf Typ ändern ein Zeichen in Android. Wenn Benutzertyp space oder - es zu _TextWatcher Warnungen und langsam in Android

ändern Also habe ich versucht:

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); 

es „funktioniert“ aber wenn die schnelle Benutzertypen einige Briefe werden nicht angezeigt, und ich habe ein paar Warnungen:

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 

irgendwelche Ideen was ist los?

Antwort

1

Keine Notwendigkeit zu entfernen und Text ändern Hörer immer wieder auf Textänderung hinzufügen, legen sich lediglich um einen Zustand zu überprüfen, ob Ihr editierbar ist mit „-“ oder „“, dann einfach ersetzen und setze es auf 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

dies erstaunlich, ich danke Ihnen sehr. –

1

Versuchen Sie, diese 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

mehr Führer in Android-Entwickler - [link] (https://developer.android.com/reference/android/text/TextWatcher.html) – ITSGuru