编辑文本密码切换Android

时间:2016-08-03 05:29:09

标签: android android-edittext gesturelistener

我正在尝试向用户显示输入类型为文本密码的编辑文本中的输入密码。

我在这样的切换图标上实现了gesturelistener -

public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (view.getId())
        {
            case R.id.ivPasswordToggle:

                switch ( motionEvent.getAction() ) {
                    case MotionEvent.ACTION_DOWN:
                        Toast.makeText(getContext(),"show",Toast.LENGTH_SHORT).show();
                        etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                        break;
                    case MotionEvent.ACTION_UP:
                        etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                        Toast.makeText(getContext(),"hide",Toast.LENGTH_SHORT).show();
                        break;
                }
                break;
        }
        return true;
    }

我不知道什么是错的,任何帮助都将不胜感激。

8 个答案:

答案 0 :(得分:23)

自支持库v24.2.0起。你可以轻松地解决这个问题

您需要做的只是:

  1. 将设计库添加到您的家属

    dependencies {
         compile "com.android.support:design:25.1.0"
    }
    
  2. TextInputEditTextTextInputLayout

    结合使用
    <android.support.design.widget.TextInputLayout
        android:id="@+id/etPasswordLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true">
    
        <android.support.design.widget.TextInputEditText
            android:id="@+id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password_hint"
            android:inputType="textPassword"/>
    </android.support.design.widget.TextInputLayout>
    
  3. passwordToggleEnabled属性将使密码切换显示

    1. 在您的根布局中,不要忘记添加xmlns:app="http://schemas.android.com/apk/res-auto"

    2. 您可以使用以下方式自定义密码切换:

    3. app:passwordToggleDrawable - 可绘制用作密码输入可见性切换图标 app:passwordToggleTint - 用于密码输入可见性切换的图标 app:passwordToggleTintMode - 用于应用背景色调的混合模式。

      TextInputLayout documentation中的更多详情。

      enter image description here

答案 1 :(得分:7)

请尝试此代码。

public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (view.getId())
        {
            case R.id.ivPasswordToggle:

                switch ( motionEvent.getAction() ) {
                    case MotionEvent.ACTION_DOWN:
                        Toast.makeText(getContext(),"show",Toast.LENGTH_SHORT).show();
                         etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        break;
                    case MotionEvent.ACTION_UP:
                         etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                        Toast.makeText(getContext(),"hide",Toast.LENGTH_SHORT).show();
                        break;
                }
                break;
        }
        return true;
    }

我希望它会起作用,谢谢。

答案 2 :(得分:2)

尝试以下方法。在这里,我们设置了一个复合drawable,当点击它时会显示或隐藏密码:

private boolean passwordShown = false;

private void addPasswordViewToggle() {
        getPasswordEditText().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int DRAWABLE_RIGHT = 2; //index

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (event.getRawX() >= (getPasswordEditText().getRight() - getPasswordEditText().getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                        if (passwordShown) {
                            passwordShown = false;
                            // 129 is obtained by bitwise ORing InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                            getPasswordEditText().setInputType(129);

                            // Need to call following as the font is changed to mono-space by default for password fields
                            getPasswordEditText().setTypeface(Typeface.SANS_SERIF);
                            getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.locked_icon, 0); // This is lock icon
                        } else {
                            passwordShown = true;
                            getPasswordEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

                            getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.unlocked_icon, 0); // Unlock icon
                        }

                        return true;
                    }
                }
                return false;
            }
        });
    }

答案 3 :(得分:1)

如果您不需要任何额外的布尔值或依赖项,那么

mapper.readValue(jsonParser, new new TypeReference<Foo<T>>() {})

    <EditText
        android:id="@+id/et_input_pass"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_marginTop="3dp"
        android:layout_marginStart="56dp"
        android:layout_marginEnd="56dp"
        android:hint="Password"
        android:inputType="textPassword"
        android:singleLine="true"
        android:textSize="13sp"
        android:background="@color/transparent"
        android:theme="@style/MyEditText" />

就是这样!

答案 4 :(得分:1)

app:passwordToggleEnabled =“ true”

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true">

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter Password"
        android:inputType="textPassword"
        android:maxLines="1" />

</com.google.android.material.textfield.TextInputLayout>

答案 5 :(得分:0)

 fragmentLoginBinding.imageViewEye.setOnClickListener(v -> {
        if (!isPasswordVisible) {
            fragmentLoginBinding.editTextPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            fragmentLoginBinding.imageViewEye.setImageDrawable(getResources().getDrawable(R.mipmap.feather_eye_crossed));
            isPasswordVisible = true;
        } else {
            fragmentLoginBinding.editTextPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            fragmentLoginBinding.imageViewEye.setImageDrawable(getResources().getDrawable(R.mipmap.feather_eye));
            isPasswordVisible = false;
        }
    });

答案 6 :(得分:0)

如果您想使用 EditText 或 AppCompatEditText,您可以通过以下方式实现所需的输出;

<androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/layout_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/spinner_role"
                >

                <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/edit_text_password"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:autofillHints="@string/password"
                    android:hint="@string/password"
                    android:inputType="textPassword"
                    android:textSize="16sp"
                    app:backgroundTint="#59A6B6"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/button_password_toggle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="4dp"
                    android:src="@drawable/ic_visibility_off"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/register_user_password_et" />

            </androidx.constraintlayout.widget.ConstraintLayout>

在您的 .kt 文件中;

if (registerUserBinding.editTextPassword.transformationMethod.equals(
            PasswordTransformationMethod.getInstance()
        )
    ) {
        registerUserBinding.registerUserPasswordEt.transformationMethod =
            HideReturnsTransformationMethod.getInstance()
        registerUserBinding.buttonPasswordToggle.setImageDrawable(
            ContextCompat.getDrawable(
                registerUserBinding.registerUserPasswordEt.context,
                R.drawable.ic_visibility
            )
        )

    } else {
        registerUserBinding.editTextPassword.transformationMethod =
            PasswordTransformationMethod.getInstance()
        registerUserBinding.buttonPasswordToggle.setImageDrawable(
            ContextCompat.getDrawable(
                registerUserBinding.registerUserPasswordEt.context,
                R.drawable.ic_visibility_off
            )
        )
    }

答案 7 :(得分:0)

<块引用>

像下面那样实现 TextInput。提供该功能的主要属性是 app:passwordToggleEnabled="true"

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/resetpasswordactivity_textinputlayout_newpassword"
                android:layout_width="0dp"
                android:layout_height="60dp"
                app:passwordToggleEnabled="true"
                android:theme="@style/loginActivityHintStyle"
                app:layout_constraintBottom_toTopOf="@+id/resetpasswordactivity_textinputlayout_newconfirmpassword"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/resetpasswordactivity_textinputlayout_resetcode"
                app:layout_constraintVertical_bias="0.15">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/resetpasswordactivity_textinputedittext_newpassword"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:hint="Password"
                    android:inputType="textPassword"
                    android:labelFor="@id/resetpasswordactivity_textinputedittext_newpassword"
                    android:maxLength="100"
                    android:textColor="@drawable/black_inputtext_color"
                    />
            </com.google.android.material.textfield.TextInputLayout>