TextInputLayout中有一个用于InputType textPassword的密码可见性切换按钮。
以某种方式可以捕捉到切换事件吗?
我找不到任何公开的方法
答案 0 :(得分:2)
我查看了TextInputLayout的源代码,以找到切换按钮的视图类型。它的CheckableImageButton。其他一切都很简单。您需要找到迭代迭代迭代TextInputLayout视图的子项的视图。然后将setOnTouchListener作为@MikeM在评论中提出。
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;
}
findTogglePasswordButton的替代实施
private View findTogglePasswordButton() {
return findViewById(R.id.text_input_password_toggle);
}
@MikeM。谢谢你的身份
答案 1 :(得分:0)
您可以使用此:
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.design.widget.CheckableImageButton;
import android.support.design.widget.TextInputLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.ctm.core.ui.R;
import org.jetbrains.annotations.NotNull;
/**
* Extended {@link TextInputLayout} allowing setting a listener when the user toggles the visibility
* of the password.
*/
public class TextInputLayoutEx extends TextInputLayout {
public TextInputLayoutEx(Context context) {
super(context);
}
public TextInputLayoutEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextInputLayoutEx(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setOnToggleShowPasswordListener(OnToggleShowPasswordListener listener) {
if (listener != null) {
initTogglePasswordShownListener(listener);
}
}
@SuppressLint({"RestrictedApi"})
private void initTogglePasswordShownListener(@NotNull OnToggleShowPasswordListener listener) {
final View revealPasswordView = this.findViewById(R.id.text_input_password_toggle);
if (revealPasswordView == null) {
return;
}
if (!(revealPasswordView instanceof CheckableImageButton)) {
return;
}
final CheckableImageButton revealPasswordCheckableImageButton = (CheckableImageButton) revealPasswordView;
revealPasswordView.setOnTouchListener((view, motionEvent) -> {
if (MotionEvent.ACTION_UP == motionEvent.getAction()) {
listener.onToggleShowPasswordClicked(!revealPasswordCheckableImageButton.isChecked());
return view.performClick();
}
return false;
});
}
public interface OnToggleShowPasswordListener {
void onToggleShowPasswordClicked(boolean isShown);
}
}
在客户端代码中:
TextInputLayoutEx layoutPassword = (TextInputLayoutEx) findByById(...)
layoutPassword.setPasswordVisibilityToggleEnabled(true);
layoutPassword.setOnToggleShowPasswordListener(isShown -> Toast.makeText(SignInActivity.this, "Password shown: " + isShown, Toast.LENGTH_SHORT).show());
答案 2 :(得分:0)
使用 Layout Inspector 分析了视图层次结构后,我可以看到以下内容:
因此您可以执行以下操作:
val toggle = password.findViewById<CheckableImageButton?>(R.id.text_input_password_toggle)
toggle?.setOnClickListener {
println("clicked on toggle")
}
或手动:
// Assuming you have AndroidKTX
val views = password.children
for (v in views) {
if (v is FrameLayout) {
val innerViews = v.children
for (iv in innerViews) {
if (iv is CheckableImageButton) {
iv.setOnClickListener {
println("clicked on toggle")
}
}
}
}
}
答案 3 :(得分:0)
我在这里使用绑定。
这是布局的ID-regPasswordlayout 密码切换的默认ID-text_input_password_toggle
binding.regPasswordlayout.findViewById(R.id.text_input_password_toggle)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// your code goes here
}
});
答案 4 :(得分:0)
自Material Components v1.1.0(科特琳)以来,您可以执行以下操作:
textInputLayout.setEndIconOnClickListener {
// do something here
}