每次更改后重置编辑文本的值


我希望在以下

行之后将editText的值设置为 null:new nwcomm.execute(cmd,cmd1),以便我可以在按下键盘上的键时发送键。我试过keyboard.setText(null);但它不起作用。我该怎么办?

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

你应该使用afterTextChanged,而不是根据我链接的文档使用onTextChanged。 这应该可以解决您在清除TextView时遇到的问题;但是,您必须小心不要这样做进入无限循环。 您必须添加对空字符串的检查,例如

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

这应该可以解决问题。

每当你想要重置edittext值时,使用editText.setText(");

最新更新