Search code examples
androidandroid-edittexttextwatcher

Resetting value of editText after each change


I want the value of editText to be set to null after the line: new nwcomm.execute(cmd,cmd1) so that I can send keys as I press them on the keyboard. I tried keyboard.setText(null); but it doesn't work. What should I do?

keyboard.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        String stt="key_";
        String key=keyboard.getText().toString();
        String cmd=stt+key;
        new nwcomm().execute(cmd,cmd1);
    }
}

Solution

  • You should be using afterTextChanged instead of onTextChanged per the documentation I linked. That should fix your problem with clearing the TextView; however, you have to be careful not to get into an infinite loop doing this. You'll have to add a check for empty string such as

    keyboard.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            String key = s.toString();
            if ( key.isEmpty() )
                return;
            String stt="key_";
            String cmd=stt+key;
            new nwcomm().execute(cmd,cmd1);
            s.clear();
    }
    

    That should to the trick.