1.创建TextWatcher实例,重写onTextChanged()方法:
TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// do something here
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
2.设置输入框的文本变化监听:
EditText editText = findViewById(R.id.editText);
editText.addTextChangedListener(textWatcher);
3.在onTextChanged()方法中实时获取输入框的文本:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String text = editText.getText().toString();
// do something here
}