我有一个应用程序,其中我有很多编辑文本,但是在某些编辑文本中,我只想实现一种文本观看者。我该怎么做,这是友善的指导。
代码: -
private TextWatcher m_textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
isFieldsEmpty();
}
@Override
public void afterTextChanged(Editable s) {
m_mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = m_mainLayout.getRootView().getHeight() - m_mainLayout.getHeight();
if (!(heightDiff > RewardUtil.dpToPx(mContext, 200))) { // if more than 200 dp, it's probably a keyboard...
m_otpEditText.clearFocus();
}
}
});
}
};
m_otpEditText.addTextChangedListener(m_textWatcher);
创建metod lke lke。
public static class MyTextWatcher implements TextWatcher {
private EditText mEditText;
public MyTextWatcher(EditText editText) {
mEditText = editText;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
oldText = mEditText.toString();
}
....
}
和这样使用...
m_otpEditText.addTextChangedListener(new MyTextWatcher(mFirstEditText));
它将为您提供帮助。谢谢。
首先,您可以创建一个类,如下:
public class CustomTextWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
}
}
此后,只需覆盖所需的方法即可。假设您仅需要ontextchanged方法,那么您可以按照以下方式进行操作:
m_otpEditText.addTextChangedListener(new CustomTextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
textwatcher类使您灵活地覆盖所需的方法。如果您确实只需要一种方法,那么只需要一种方法,但是您必须始终实现所有方法。