public class EditText extends TextView {
public EditText(Context context) {
this(context, null);
}
/**
* textview 内容发生变化后,回调该方法,需要子类实现
* @param text 变化后的内容
* @param start 从哪个位置开始发生变化的
* @param lengthBefore 删除的字符个数(增加内容该值为0)
* @param lengthAfter 增加的字符个数(删除内容该值为0)
*/
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
// intentionally empty, template pattern method can be overridden by subclasses
}
}
demo演示:
@SuppressLint("AppCompatCustomView")
public class CustomEditText extends EditText {
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
Log.d("CustomEditText", "CharSequence text: " + text + " ||int start " + start + " ||int lengthBefore: " + lengthBefore + " ||int lengthAfter: " + lengthAfter);
}
}
复制字符串:abcd 然后再输入 2
09-03 19:29:44.005 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd int start 0 int lengthBefore: 0 int lengthAfter: 4
09-03 19:30:00.883 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd2 int start 4 int lengthBefore: 0 int lengthAfter: 1
删除 2
09-03 19:29:44.005 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd int start 0 int lengthBefore: 0 int lengthAfter: 4
09-03 19:30:00.883 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd2 int start 4 int lengthBefore: 0 int lengthAfter: 1
09-03 19:31:01.029 6137-6137/com.crg.androidtest D/CustomEditText: CharSequence text: abcd int start 4 int lengthBefore: 1 int lengthAfter: 0
demo和我们上面的结论一致
public class MainActivity extends AppCompatActivity {
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = findViewById(R.id.edittext);
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d("TAG", "TAG:== CharSequence beforeTextChanged: s= " + s + " ||int start " + start + " ||int count: " + count + " ||int after: " + after + "\n");
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("TAG", "TAG:== CharSequence onTextChanged: s= " + s + " ||int start " + start + " ||int before: " + before + " ||int count: " + count + "\n");
}
@Override
public void afterTextChanged(Editable s) {
Log.d("TAG", "TAG:== CharSequence afterTextChanged s= " + s.toString());
}
});
}
}
测试demo:
复制字符串:abcd 再输入 3
09-03 19:57:18.849 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence beforeTextChanged: s= ||int start 0 ||int count: 0 ||int after: 4
09-03 19:57:18.853 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence onTextChanged: s= abcd ||int start 0 ||int before: 0 ||int count: 4
09-03 19:57:18.868 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged s= abcd
09-03 19:57:32.759 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence beforeTextChanged: s= abcd ||int start 4 ||int count: 0 ||int after: 1
09-03 19:57:32.760 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence onTextChanged: s= abcd3 ||int start 4 ||int before: 0 ||int count: 1
09-03 19:57:32.776 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged s= abcd3
然后删除 3
09-03 19:57:18.849 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence beforeTextChanged: s= ||int start 0 ||int count: 0 ||int after: 4
09-03 19:57:18.853 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence onTextChanged: s= abcd ||int start 0 ||int before: 0 ||int count: 4
09-03 19:57:18.868 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged s= abcd
09-03 19:57:32.759 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence beforeTextChanged: s= abcd ||int start 4 ||int count: 0 ||int after: 1
09-03 19:57:32.760 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence onTextChanged: s= abcd3 ||int start 4 ||int before: 0 ||int count: 1
09-03 19:57:32.776 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged s= abcd3
09-03 19:59:53.353 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence beforeTextChanged: s= abcd3 ||int start 4 ||int count: 1 ||int after: 0
09-03 19:59:53.354 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence onTextChanged: s= abcd ||int start 4 ||int before: 1 ||int count: 0
09-03 19:59:53.356 8727-8727/com.crg.androidtest D/TAG: TAG:== CharSequence afterTextChanged s= abcd
TextWatcher 回调参数详解
TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// s 未改变之前的内容
// start 内容被改变的的开始位置
// count 原始文字被删除的个数
// after 新添加的文字个数
//========================= start 和 count 结合 获取被删除的文字 ===================
// String deleteText = s.toString().substring(start + 1 - count, start + 1);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// s 改变之后的内容
// start 内容被改变的的开始位置
// before 原始文字被删除的个数
// count 新添加的文字个数
//========================= start 和 count 结合 获取新添加的文字 ===================
String addText = s.toString().substring(start, start + count);
}
@Override
public void afterTextChanged(Editable s) {
// s 改变之后的最终内容
}
};
demo源码地址