当编辑文本不清晰时,使用textinputlayout隐藏密码切换

时间:2018-08-01 12:07:43

标签: android android-textinputlayout textinputlayout

这是我在项目中使用的代码:

<android.support.design.widget.TextInputLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/signup_input_full_name_wrapper"
        android:layout_marginEnd="@dimen/signup_edit_text_input_fullname_margin_right"
        android:layout_marginRight="@dimen/signup_edit_text_input_fullname_margin_right"
        android:layout_marginStart="@dimen/signup_edit_text_input_fullname_margin_left"
        android:layout_marginLeft="@dimen/signup_edit_text_input_fullname_margin_left"
        android:layout_marginTop="@dimen/signup_edit_text_input_fullname_margin_top"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/signup_input_password_wrapper">
    <EditText
        android:id="@+id/signup_input_full_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Full Name"
        android:textSize="@dimen/signup_edit_text_input_fullname_text_size"
        android:textColorHint="@color/signup_edit_text_hint_color"
         />
</android.support.design.widget.TextInputLayout>

这可以像说的那样进行工作,即它为我提供了嵌套编辑文本的密码切换功能。 我想要的是当编辑文本没有焦点时是否可以隐藏它?

2 个答案:

答案 0 :(得分:1)

这可能不是最佳做法,而是它的工作。

您的xml:

           <android.support.design.widget.TextInputLayout
                android:id="@+id/etPasswordLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:passwordToggleEnabled="false">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/etPassword"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="password"
                    android:inputType="textPassword"/>
            </android.support.design.widget.TextInputLayout>

您的活动或片段:

etPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean isFocused) {
                if(!isFocused){
                    etPasswordLayout.setPasswordVisibilityToggleEnabled(false);
                }else{
                    etPasswordLayout.setPasswordVisibilityToggleEnabled(true);
                }
            }
        });

答案 1 :(得分:0)

我执行了这里所说的内容,并对要发布的内容进行了一些更改。

private EditText etPassword;
private TextInputLayout etPasswordLayout;
//View componenets initialization
    etPassword = findViewById(R.id.signup_input_password);
    etPasswordLayout = findViewById(R.id.signup_input_password_wrapper);


    //toggling the password view functionality
    etPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean isFocused) {
            if(!isFocused){
                etPasswordLayout.setPasswordVisibilityToggleEnabled(false);
            }else{

                etPassword.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        if(etPassword.getText().toString().length() > 0) {
                            etPasswordLayout.setPasswordVisibilityToggleEnabled(true);
                        }
                        else{
                            etPasswordLayout.setPasswordVisibilityToggleEnabled(false);
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable editable) {

                    }
                });

            }
        }
    });

现在,只有当密码编辑文本处于焦点时,并且仅当用户输入了内容时,密码切换才可见。