TextInputLayout 中有一个用于输入类型 textPassword 的密码可见性切换按钮。
是否有可能捕获切换事件?
我找不到任何公共方法
我查看了 TextInputLayout 的源代码以找到切换按钮的视图类型。它的 CheckableImageButton。其他一切都很容易。您需要找到在 TextInputLayout 视图的子级上递归迭代的视图。然后按照 @MikeM 在评论中建议的那样 setOnTouchListener 。
View togglePasswordButton = findTogglePasswordButton(mTextInputLayoutView);
if (togglePasswordButton != null) {
togglePasswordButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// implementation
return false;
}
});
}
private View findTogglePasswordButton(ViewGroup viewGroup) {
int childCount = viewGroup.getChildCount();
for (int ind = 0; ind < childCount; ind++) {
View child = viewGroup.getChildAt(ind);
if (child instanceof ViewGroup) {
View togglePasswordButton = findTogglePasswordButton((ViewGroup) child);
if (togglePasswordButton != null) {
return togglePasswordButton;
}
} else if (child instanceof CheckableImageButton) {
return child;
}
}
return null;
}
查找切换密码按钮的替代实现
private View findTogglePasswordButton() {
return findViewById(R.id.text_input_password_toggle);
}
@迈克M。谢谢你的身份证
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)