TextInputLayout密码切换监听器

时间:2018-02-28 20:05:12

标签: android android-textinputlayout

我有一个TextInputLayout用于输入密码。我添加了passwordToggleEnabled = true来切换密码可见性。 当用户切换密码可见性时,我需要捕获事件。我怎么能这样做。

<android.support.design.widget.TextInputLayout
    android:id="@+id/password_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="start|center"
    app:passwordToggleEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/password_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/enter_new_password"
            android:inputType="textPassword"/>

</android.support.design.widget.TextInputLayout>

4 个答案:

答案 0 :(得分:3)

TextInputLayout的来源中,切换按钮的视图类型为CheckableImageButton。您只需要查找以TextInputLayout视图的子项递归迭代的视图。然后是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;
}

答案 1 :(得分:0)

像在另一个答案中建议的那样仅设置结束图标单击侦听器将不起作用,因为这样做将删除用于切换EditText转换方法的现有侦听器。

因此,如果您设置了新的侦听器,则需要自己进行更改:

textInputLayout.setEndIconOnClickListener {
    // Toggle the EditText transformation method from nothing to password or vice versa.
    // Selection is lost in the process so make sure to restore it.
    val editText = textInputLayout.editText
    val oldSelection = editText.selectionEnd
    val hidePassword = editText.transformationMethod !is PasswordTransformationMethod
    passwordEdt.transformationMethod = PasswordTransformationMethod.getInstance().takeIf { hidePassword }
    if (oldSelection >= 0) {
        passwordEdt.setSelection(oldSelection)
    }

    // Do your own stuff here.
}

答案 2 :(得分:0)

如果您仍在寻找有关TextInputLayout密码切换侦听器的完整解决方案,请检查此答案:

let HeaderRight = () => favorit ? (
    <MaterialIcons
        style={styles.icon}
        name={'favorite'}
        size={33}
        color={'red'}
        onPress={() => {
            setFavorit(_delFromDB(lokal.id));
        }}
    />
) : (
    <MaterialIcons
        style={styles.icon}
        name={'favorite'}
        size={33}
        color={'red'}
        onPress={() => {
            setFavorit(_saveToDB(lokal.id));
        }}
    />
);

React.useLayoutEffect(() => {
    navigation.setOptions({
        headerRight: () => <HeaderRight />,
    });
}, [navigation]);

设置EndIconOnClickListener时,需要同时检查TextInputEditText文本状态和TextInputLayout EndIcon Drawable文件。这样您就可以管理这种情况。

答案 3 :(得分:-1)

请参阅my answer here,但是总而言之,您可以从Material Components v1.1.0(科特琳)开始执行以下操作:

textInputLayout.setEndIconOnClickListener {
    // do something here
}