EditText的焦点问题。
需求背景:
1.EditText的输入线条 未输入状态时,灰色。
2.输入状态时,绿色;
3.点击“确定”且 输入格式错误时(包含空),红线提示。一旦重获焦点,变成输入状态的绿色。
实现效果如下:
/**
* 焦点在的时候,输入线条变绿
*
* @param isColorFocus
*/
public AutoEditText setLineColorFocus(boolean isColorFocus) {
if (!isColorFocus) {
editText.setOnFocusChangeListener(null);
lineBottom.setBackgroundResource(R.color.color_e8e9eb);
//UI 特别要求,当横线颜色为灰色时高度设为0.5dp,一旦成为其他颜色就变成1dp。
ViewGroup.LayoutParams layoutParams = lineBottom.getLayoutParams();
layoutParams.height = getResources().getDimensionPixelOffset(R.dimen.dp_05);
lineBottom.setLayoutParams(layoutParams);
} else {
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
lineBottom.setBackgroundResource(hasFocus ? R.color.color_00b38a : R.color.color_e8e9eb);
int height = hasFocus ? getResources().getDimensionPixelOffset(R.dimen.dp_1) : getResources().getDimensionPixelOffset(R.dimen.dp_05);
ViewGroup.LayoutParams layoutParams = lineBottom.getLayoutParams();
layoutParams.height = height;
lineBottom.setLayoutParams(layoutParams);
}
});
}
return this;
}
当输入格式错误时,调用下面的方法
/**
* 错误时候 输入线 变红
*
* @return
*/
public AutoEditText setLineColorError() {
editText.clearFocus();
if (context != null && context instanceof Activity) {
InputMethodUtil.hideKeyboard((Activity) context);
}
lineBottom.setBackgroundResource(R.color.color_ff945a);
ViewGroup.LayoutParams layoutParams = lineBottom.getLayoutParams();
layoutParams.height = getResources().getDimensionPixelOffset(R.dimen.dp_1);
lineBottom.setLayoutParams(layoutParams);
return this;
}
然而 一旦变红之后 ,随便点击EditText无法变绿了。debug看一下onFocusChange(View v, boolean hasFocus),发现editText.clearFocus()时 该监听压根不会走的。恍然大悟,对!对!对!是EditText的父控件没有设置setFocusable(true)和setFocusableInTouchMode(true),
赶紧设置后,run一把,没用!
会不会是需要在Activity的根布局加上呢? 然后 初始化方法里加上这个
if (null != context && context instanceof Activity) {
View root = ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
root.setFocusable(true);
root.setFocusableInTouchMode(true);
}
然并卵 依然没用。没有骂娘,心态很好。
龙哥说,需要加上editText.setFocusable(fasle),于是代码改为这样
/**
* 错误时候 输入线 变红
*
* @return
*/
public AutoEditText setLineColorError() {
editText.setFocusable(false);
editText.post(() -> {
editText.setFocusableInTouchMode(true);
});
if (context != null && context instanceof Activity) {
InputMethodUtil.hideKeyboard((Activity) context);
}
lineBottom.setBackgroundResource(R.color.color_ff945a);
ViewGroup.LayoutParams layoutParams = lineBottom.getLayoutParams();
layoutParams.height = getResources().getDimensionPixelOffset(R.dimen.dp_1);
lineBottom.setLayoutParams(layoutParams);
return this;
}
试了下 ,果然,果然 ,还是没用。没骂娘,耐心的继续找原因。
写个demo发现能行。保持优雅,没有再三骂娘。
然后加上了一行代码setLineColorFocus(true);即重新设置一遍editText.setOnFocusChangeListener,竟然莫名其妙的好了。
最终代码如下:
/**
* 错误时候 输入线 变红
*
* @return
*/
public AutoEditText setLineColorError() {
editText.setFocusable(false);
editText.post(() -> {
editText.setFocusableInTouchMode(true);
setLineColorFocus(true);
});
if (context != null && context instanceof Activity) {
InputMethodUtil.hideKeyboard((Activity) context);
}
lineBottom.setBackgroundResource(R.color.color_ff945a);
ViewGroup.LayoutParams layoutParams = lineBottom.getLayoutParams();
layoutParams.height = getResources().getDimensionPixelOffset(R.dimen.dp_1);
lineBottom.setLayoutParams(layoutParams);
return this;
}
立帖为证,Demo上没重新设置监听是可以的,项目中 不重新设置就不行。悬疑,求路过大神指津。