我在自定义布局中使用许多内部EditText,当我单独设置重力并且它有一个TextWatcher时,值不会改变。
public PinFieldView(Context context) {
super(context);
init();
}
private void init() {
setText(defaultValue);
setTextColor(Color.GRAY);
setCursorVisible(false);
setOnFocusChangeListener(focusListener);
addTextChangedListener(textWatcher);
setGravity(Gravity.CENTER_HORIZONTAL);
}
private final TextWatcher textWatcher = new TextWatcher() {
...
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
removeTextChangedListener(textWatcher);
if (s.length() == 0) {
setText(defaultValue);
} else {
setText(String.valueOf(s.charAt(s.length() - 1)));
}
if (numberChangeListener != null) numberChangeListener.onNumberChanged(getText().toString());
addTextChangedListener(textWatcher);
}
...
};
不确定是什么问题,我在Android 5.0上测试