实时编辑用户input
当用户input数据时,是否可以自动将字符插入到EditText
中 ?
也就是说,如果用户input一个很长的数字,例如123456789012
,这个数字是否可以在编辑文本框中input,但是每4个字符都input一个破折号?
所以当你input上面的数字时,你会看到它被input到EditText
框中,但看起来像这样:1234-5678-9012。
目前我有一个应用程序,你可以input一个长号码,然后按一个button,它为你插入破折号,但我很好奇,如果它可以完成,因为你键入?
非常感谢您的帮助。
通过标记android,我认为你正在讨论关于android editText,所以你可以通过监听TextChangedListener来做到这一点,
编辑:退格
editText.addTextChangedListener(new TextWatcher() { int len=0; @Override public void afterTextChanged(Editable s) { String str = editText.getText().toString(); if(str.length()==4&& len <str.length()){//len check for backspace editText.append("-"); } } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { String str = editText.getText().toString(); len = str.length(); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } });
为了解决这个问题,我写了一个类“AutoAddTextWatcher”:
1.自动插入文本到EditText中。
2.在设置的位置插入文本到EditText中。
3.当文本长度大于1时,在设置的位置删除EditText中的文本。
代码片段:
mEditText_birthday.addTextChangedListener(new AutoAddTextWatcher(mEditText_birthday, "/", new TextWatcher() {}, 4, 6));
AutoAddTextWatcher类
import android.text.Editable; import android.text.TextWatcher; import android.widget.EditText; /** * Created by henry.chuang on 2016/5/12. */ public class AutoAddTextWatcher implements TextWatcher { private CharSequence mBeforeTextChanged; private TextWatcher mTextWatcher; private int[] mArray_pos; private EditText mEditText; private String mAppentText; public AutoAddTextWatcher(EditText editText, String appendText, int... position){ this.mEditText = editText; this.mAppentText = appendText; this.mArray_pos = position.clone(); } public AutoAddTextWatcher(EditText editText, String appendText, TextWatcher textWatcher, int... position){ this(editText, appendText, position); this.mTextWatcher = textWatcher; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { mBeforeTextChanged = s.toString(); if(mTextWatcher != null) mTextWatcher.beforeTextChanged(s, start, count, after); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { for (int i = 0; i < mArray_pos.length; i++) { if(((mBeforeTextChanged.length() - mAppentText.length() * i) == (mArray_pos[i] - 1) && (s.length() - mAppentText.length() * i) == mArray_pos[i])){ mEditText.append(mAppentText); break; } if(((mBeforeTextChanged.length() - mAppentText.length() * i) == mArray_pos[i] && (s.length() - mAppentText.length() * i) == (mArray_pos[i] + 1))){ int idx_start = mArray_pos[i] + mAppentText.length() * i; int idx_end = Math.min(idx_start + mAppentText.length(), s.length()); String sub = mEditText.getText().toString().substring(idx_start, idx_end); if(!sub.equals(mAppentText)){ mEditText.getText().insert(s.length() - 1, mAppentText); } break; } if(mAppentText.length() > 1 && (mBeforeTextChanged.length() - mAppentText.length() * i) == (mArray_pos[i] + mAppentText.length()) && (s.length() - mAppentText.length() * i) == (mArray_pos[i] + mAppentText.length() - 1)){ int idx_start = mArray_pos[i] + mAppentText.length() * i; int idx_end = Math.min(idx_start + mAppentText.length(), s.length()); mEditText.getText().delete(idx_start, idx_end); break; } } if(mTextWatcher != null) mTextWatcher.onTextChanged(s, start, before, count); } @Override public void afterTextChanged(Editable s) { if(mTextWatcher != null) mTextWatcher.afterTextChanged(s); } }
完整的演示源码:
https://github.com/henrychuangtw/AutoInsertEditText
@Override public void afterTextChanged(Editable s) { if(s.length() == 3 && len < s.length()){ s.append(" - "); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { len = s.length(); }
这样做,只有这个代码会在第三个字符之后插入" - "
。
这是我用的
private boolean mInEdit; @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (!mInEdit) { mInEdit = true; String delimiter = " - "; //Remove chars from your delimiter first String digits = s.toString().replaceAll("[- ]", "") .replaceAll("\\d{4}", "$0" + delimiter); //Handle deletion int dLength = delimiter.length(); if (before > count && digits.endsWith(delimiter.charAt(dLength - 1)) { digits = digits.substring(0, digits.length() - dLength); } mCardNumber.setText(digits); mCardNumber.setSelection(mCardNumber.length()); mInEdit = false; } }
在这里你用你想要分隔的数字来代替分隔符。
对于那些仍然面临退格和多个连字符的问题 –
new TextWatcher() { boolean hyphenExists; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { if (s.length() >= 6 && s.charAt(5) == '-') { hyphenExists = true; } else { hyphenExists = false; } Log.d("TAG", "beforeTextChanged " + s.toString()); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("TAG", "onTextChanged " + s.toString()); } @Override public void afterTextChanged(Editable s) { if (s.length() == 5) { if (!hyphenExists) s.append('-'); } Log.d("TAG", "afterTextChanged " + s.toString()); } }