Android Password toggle want to set text instead of image

限于喜欢 提交于 2020-01-17 08:42:10

问题


I am using TextInputEditText as a password field. I want to use password toggle feature available with support library 24.2.0. passwordToggleDrawable allows me to set drawable to change based on toggle state. I want to show TextView instead of drawable to show toggle state i.e. I want show text 'SHOW' to allow password to be visible and 'HIDE' when user wants to hide password. I could see property 'passwordToggleContentDescription' that takes text. I tried to set this property with as '@null', but it doesn't show text.

Below is code I am trying,

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="16dp"
        app:passwordToggleEnabled="true"
        app:passwordToggleDrawable="@null"
        app:passwordToggleContentDescription="Test1">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:hint="Password"
            android:imeOptions="actionNext"
            android:inputType="textPassword"
            android:maxLength="75"
            android:textCursorDrawable="@null" />
    </android.support.design.widget.TextInputLayout>

回答1:


I am achieving this using framelayout. I am showing TextView on top of TextInputEditText

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="allstate.com.testfab.MainActivity"
    tools:showIn="@layout/activity_main">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="16dp"
        app:passwordToggleEnabled="true"
        app:passwordToggleDrawable="@null"
        app:passwordToggleContentDescription="Test1">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:hint="Password"
            android:imeOptions="actionNext"
            android:inputType="textPassword"
            android:maxLength="10"
            android:textCursorDrawable="@null" />
    </android.support.design.widget.TextInputLayout>

    <TextView
        android:id="@+id/toggle_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:text="SHOW"
        android:layout_marginRight="16dp"
        android:paddingTop="10dp"
        />
</FrameLayout>

I am handling onclicks on TextView as below,

textView = (TextView) findViewById(R.id.toggle_button);
        textView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                visible = !visible;
                if (visible) {
                    textView.setText("HIDE");
                    editText.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    textView.setText("SHOW");
                    editText.setInputType(TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD);
                }
                editText.setSelection(editable.toString().length());
            }
        });

Please let me know if you find better solution than this.



来源:https://stackoverflow.com/questions/44183004/android-password-toggle-want-to-set-text-instead-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!