Change to custom icon from eye-icon(default) for h

2020-02-23 08:13发布

I want to change/display different icons for show password in android edittext. I am using following code to display icon.

<android.support.design.widget.TextInputLayout
    android:id="@+id/layoutTextInput"
    android:textColorHint="@color/aluminium">
    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/editTextValue"
        android:imeOptions="actionNext"
        android:layout_marginBottom="8dp"
        android:inputType="text"/>
</android.support.design.widget.TextInputLayout>

I want to use custom icons instead of normal icons(eye-icon). Please help me.

8条回答
▲ chillily
2楼-- · 2020-02-23 08:41
**Add Dependency**

compile 'com.android.support:design:25.0.1'
    compile 'com.android.support:support-v4:25.0.1'
    compile 'com.android.support:appcompat-v7:25.0.1'
    compile 'com.android.support:support-vector-drawable:25.0.1'

  <android.support.design.widget.TextInputLayout
        android:id="@+id/layout_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/layout_email"
        android:textColorHint="@color/colorHint"

app:passwordToggleEnabled="true">

        <EditText
            android:id="@+id/editTextPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="45dp"
            android:hint="@string/hint_password"
            android:inputType="textPassword"
            android:textColor="@color/textColor" />
    </android.support.design.widget.TextInputLayout>
查看更多
一纸荒年 Trace。
3楼-- · 2020-02-23 08:42

In your Xml Make a RelativeLayout with TextInputLayout and Imageview

 <RelativeLayout
            android:id="@+id/relative"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/relativeone"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:orientation="horizontal"
            android:weightSum="5">

            <android.support.design.widget.TextInputLayout>
                android:id="@+id/layoutTextInput"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_gravity="center_vertical"
                android:layout_weight="5"
                android:gravity="center_vertical"
                android:hint="Password"
                android:paddingTop="4dp"
                android:textColorHint="#3f3f3f">

                <EditText
                    android:id="@+id/passwordedit"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:drawableLeft="@mipmap/bluelocked"
                    android:drawablePadding="13dp"
                    android:gravity="center_vertical"
                    android:inputType="textPassword"
                    android:paddingLeft="15dp"
                    android:textColor="#3f3f3f"
                    android:textColorHint="#3f3f3f"
                    android:textSize="13sp" />

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

            <ImageView
                android:id="@+id/imagepassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_gravity="center_vertical"
                android:paddingBottom="20dp"
                android:paddingRight="10dp"
                android:src="@mipmap/IMAGEYOUWANT" />

        </RelativeLayout>

And in your Activity add onTouch() method for showing and hidding your password.

    ImageView imagepass = (ImageView) findViewById(R.id.imagepassword);
    imagepass .setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    editpass.setInputType(InputType.TYPE_CLASS_TEXT);
                    break;
                case MotionEvent.ACTION_UP:
                    editpass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    break;
            }
            return true;
        }
    });
查看更多
smile是对你的礼貌
4楼-- · 2020-02-23 08:42
1.create a xml layout...I have created in my way
<FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_50dp"
                    android:layout_marginTop="@dimen/_15dp">

                    <android.support.v7.widget.AppCompatEditText
                        android:id="@+id/etCurrentPassword"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/drawable_edit_text_2"
                        android:fontFamily="@font/poppins"
                        android:hint="@string/old_password"
                        android:inputType="textPassword"
                        android:padding="@dimen/_10dp" />

                    <android.support.v7.widget.AppCompatImageView
                        android:id="@+id/imgShowPassword1"
                        android:layout_width="@dimen/_50dp"
                        android:layout_height="@dimen/_50dp"
                        android:layout_gravity="end|center_vertical"
                        android:layout_marginRight="@dimen/_10sp"
                        android:padding="@dimen/_12dp"
                        android:tint="@android:color/darker_gray"
                        app:srcCompat="@drawable/ic_eye" />
                </FrameLayout>

2.In activity class: call the method with the mathing parameters
showORhidePass(view,etRetypePassword,showPassword3 = !showPassword3);

void showORhidePass(AppCompatImageView imageView, AppCompatEditText editText, Boolean save){

        if (TextUtils.isEmpty(editText.getText())){
            return;
        }

        if (save){
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            editText.setSelection(editText.length());
            imageView.setColorFilter(ContextCompat.getColor(requireContext(), android.R.color.darker_gray));
        }else{
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            editText.setSelection(editText.length());
            imageView.setColorFilter(ContextCompat.getColor(requireContext(), R.color.colorPrimary));
        }

    }
查看更多
Anthone
5楼-- · 2020-02-23 08:52

I implement it using text.. that is 'Show' and 'Hide'

textViewShowPassword.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (textViewShowPassword.getText().toString().equalsIgnoreCase("Show")) {
                editTextPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                textViewShowPassword.setText("Hide");
                editTextPassword.setSelection(editTextPassword.getText().length());
            } else {
                textViewShowPassword.setText("Show");
                editTextPassword.setTransformationMethod(new PasswordTransformationMethod());
                editTextPassword.setSelection(editTextPassword.getText().length());
            }

        }
    });

Hope this help for you..!!

查看更多
一纸荒年 Trace。
6楼-- · 2020-02-23 08:56

Create a new drawable file and named it as show_password_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_visibility_black_18dp" android:state_checked="true"/>
<item android:drawable="@drawable/ic_visibility_off_black_18dp"/>
</selector>

and in your layout file, add app:passwordToggleDrawable attribute in TextInputLayout :

<android.support.design.widget.TextInputLayout
    android:id="@+id/layoutTextInput"
    app:passwordToggleEnabled="true"
    app:passwordToggleDrawable="@drawable/show_password_selector"
    android:textColorHint="@color/gray">
    <android.support.v7.widget.AppCompatEditText
        android:id="@+id/editTextValue"
        android:imeOptions="actionNext"
        android:layout_marginBottom="8dp"
        android:inputType="text"/>
</android.support.design.widget.TextInputLayout>

For Reference: https://www.youtube.com/watch?v=dW0YIV0Z9qk

查看更多
太酷不给撩
7楼-- · 2020-02-23 08:58

With AndroidX and Android Material all you need is -> app:passwordToggleEnabled="true"

`    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/TextInputLayout_password"
        style="@style/TextInputLayoutStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="24dp"
        android:hint="Password"
        app:passwordToggleEnabled="true">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/password_EditText"
            style="@style/EditText_Common_style"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:imeOptions="actionDone"
            android:inputType="textPassword"
            app:errorEnabled="true" />
    </com.google.android.material.textfield.TextInputLayout>`
查看更多
登录 后发表回答