首页 文章

如何使用TextWatcher删除EditText中的文本?每次它检测到一个字符串并格式化它

提问于
浏览
6

我有一个带TextWatcher的EditText . 它应该将文本格式化为人类高度,如5'9“ . 但是当用户犯了错误并想删除输入错误的字符时,TextWather不允许他这样做 . 假设用户想删除”characther the TextWather立即加回 . 下面是代码 . 那么如何允许用户删除EditText中的文本?

private class CustomTextWatcher implements TextWatcher {
    private EditText mEditText;

public CustomTextWatcher(EditText e) {
    mEditText = e;
}

public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
}

public void onTextChanged(CharSequence s, int start, int before,
        int count) {


}

public void afterTextChanged(Editable s) {
    int count = s.length();
    String str = s.toString();
    if (count == 1) {
        str = str + "'";
    } else if (count == 3) {
        str = str + "\"";
    } else if ((count > 4) && (str.charAt(str.length() - 1) != '\"')) {
        str = str.substring(0, str.length() - 2)
                + str.charAt(str.length() - 1) + "\"";
    } else {
        return;
    }
    mEditText.setText(str);
    mEditText.setSelection(mEditText.getText().length());


}

}

2 回答

  • 2

    忽略Karakuri发布的有关您的代码处于错误回调中的事实,您可以添加一个简单的修复程序,您只需要听取用户使用的密钥 .

    如果没有对现有代码进行任何实际测试或进一步改进,这似乎可以解决您描述的问题:

    package com.example.testwatchertest;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.View.OnKeyListener;
    import android.widget.EditText;
    
    public class MainActivity extends Activity implements TextWatcher {
    
        EditText editText;
        boolean keyDel = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            editText = (EditText) findViewById(R.id.editText);
            editText.addTextChangedListener(this);
    
            editText.setOnKeyListener(new OnKeyListener() {
    
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
    
                    if (keyCode == KeyEvent.KEYCODE_DEL){
                        keyDel = true;
                    }else{
                        keyDel = false;
                    }
                    return false;
                }
            });
    
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            if (!keyDel) {
    
                String str = s.toString();
                if (count == 1) {
                    str = str + "'";
                } else if (count == 3) {
                    str = str + "\"";
                } else if ((count > 4) && (str.charAt(str.length() - 1) != '\"')) {
                    str = str.substring(0, str.length() - 2) + str.charAt(str.length() - 1) + "\"";
                } else {
                    return;
                }
                editText.setText(str);
                editText.setSelection(editText.getText().length());
    
            }
    
        }
    
    }
    
  • 8

    From the docs

    public abstract void onTextChanged(CharSequence s,int start,int before,int count)调用此方法以通知您,在s中,从start开始的计数字符刚刚替换了之前具有长度的旧文本 . 尝试从此回调对s进行更改是错误的 .

    您应该在afterTextChanged()中进行任何更改,而不是在其他两个回调中进行任何更改 .

    public abstract void afterTextChanged(Editable s)调用此方法以通知您,在s中的某个位置,文本已更改 . 从这个回调中对s进行进一步的更改是合理的,但要注意不要让自己进入无限循环,因为你所做的任何更改都会导致以递归方式再次调用此方法 . (你不知道发生了什么变化,因为其他的afterTextChanged()方法可能已经进行了其他更改并使偏移无效 . 但是如果你需要知道这里,你可以在onTextChanged中使用setSpan(Object,int,int,int) (CharSequence,int,int,int)标记您的位置,然后从此处查找 Span 最终的位置 . )

相关问题