简体   繁体   English

在 android 中的眼睛图标上显示和隐藏密码

[英]Show and hide password on eye icon in android

Here I am creating login screen, where I have to enter username and password, In password field I have set on eye icon.在这里我正在创建登录屏幕,我必须在其中输入用户名和密码,在密码字段中我设置了眼睛图标。 While typing password will be hide and after clicking on eye icon it should show the real characters.虽然输入密码将被隐藏,点击眼睛图标后它应该显示真实的字符。 I have mention my XML and java code both.我已经提到了我的 XML 和 java 代码。 Please reveiew and help me with the solution.Do I need to add anything in gradle?请查看并帮助我解决问题。我需要在 gradle 中添加任何内容吗? Please sugggest.请建议。 Thanks谢谢

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context="com.tecnotree.selfcare.ui.login.LoginActivity">

    <VideoView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:visibility="gone"
        android:id="@+id/background_video_view"
        />
    <!-- Login progress -->

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/tecnotree_selfcare"
        android:src="@drawable/login_background"
        android:scaleType="centerCrop"/>

    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/login_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/email_login_form"
            android:layout_width="match_parent"
            android:padding="@dimen/paddingLarge"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="0dp"
                android:layout_marginRight="0dp"
                android:background="@drawable/sc_card_rounded_login"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="@dimen/marginLarge"
                    android:layout_marginBottom="@dimen/marginLarge"
                    android:contentDescription="@string/tecnotree_selfcare"
                    android:src="@drawable/logo" />

                <TextView
                    style="@style/AppTheme.TextWhiteLargeBold"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Simple, Secure and Convenient"
                    android:textAlignment="center"
                    android:textSize="22dp"
                    android:paddingLeft="12dp"
                    android:paddingRight="12dp"
                    android:paddingTop="15dp"
                    />

                <TextView
                    style="@style/AppTheme.TextWhiteSmallRegular"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/margin"
                    android:text="The convenience of managing your account at your fingertips"
                    android:textAlignment="center"
                    android:textSize="15dp"
                    android:paddingLeft="40dp"
                    android:paddingRight="40dp"
                    />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="@dimen/marginLarge"
                    android:background="#fff"
                    android:layout_marginLeft="40dp"
                    android:layout_marginRight="40dp">
                </LinearLayout>

                <TextView
                    style="@style/AppTheme.TextWhiteLargeBold"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/margin"
                    android:text="Login to Selfcare"
                    android:textAlignment="center"
                    android:textSize="24dp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginTop="@dimen/marginLarge"
                    android:layout_marginLeft="38dp"
                    android:layout_marginRight="38dp"
                    >

                    <AutoCompleteTextView
                        android:id="@+id/email"
                        style="@style/AppTheme.EditTextAuto"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:drawableStart="@drawable/user_name_icon"
                        android:drawableLeft="@drawable/user_name_icon"
                        android:drawablePadding="@dimen/padding"
                        android:hint="@string/prompt_email_mobile"
                        android:textColorHint="@color/colorLight"
                        android:inputType="textEmailAddress"
                        android:maxLines="0"
                        android:singleLine="true"
                        />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/marginLarge"
                    android:layout_marginLeft= "38dp"
                    android:layout_marginRight="38dp">

                    <EditText
                        android:id="@+id/password"
                        style="@style/AppTheme.EditText"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:drawableStart="@drawable/enter_pswrd_icon"
                        android:drawableLeft="@drawable/enter_pswrd_icon"
                        android:drawableEnd="@drawable/see_pswrd"
                        android:drawablePadding="@dimen/padding"
                        android:hint="@string/prompt_password"
                        android:textColorHint="@color/colorLight"
                        android:imeActionId="6"
                        android:imeActionLabel="@string/action_sign_in_short"
                        android:imeOptions="actionUnspecified"
                        android:inputType="textPassword"
                        android:maxLines="0"
                        android:singleLine="true" />


                </LinearLayout>


                <Button
                    android:id="@+id/email_sign_in_button"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/action_sign_in"
                    android:textAllCaps="false"
                    android:layout_marginTop="@dimen/marginLarge"
                    android:textColor="@android:color/white"
                    android:textSize="20sp"
                    android:layout_marginLeft="40dp"
                    android:layout_marginRight="40dp"

                    />

                <ProgressBar
                    android:id="@+id/progressBar2"
                    style="?android:attr/progressBarStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:visibility="gone"/>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="@dimen/marginLarge"
                    android:orientation="horizontal"
                    android:layout_marginBottom="15dp">



                    <TextView
                        android:id="@+id/register_now_button"
                        style="@style/AppTheme.TextWhiteLargeRegular"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingLeft="@dimen/paddingSmall"
                        android:text="Sign Up"
                        android:textSize="15dp"
                        android:layout_marginRight="40dp"/>

                    <TextView
                        android:id="@+id/forgot_password_button"
                        style="@style/AppTheme.TextWhiteLargeRegular"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"

                        android:text="Forgot your password?"
                        android:textAlignment="center"
                        android:textSize="15dp"
                        android:layout_marginLeft="40dp"/>
                </LinearLayout>

            </LinearLayout>


        </LinearLayout>
    </ScrollView>
</RelativeLayout>

// Java Code for login activity.. // Java 登录活动代码..

package com.tecnotree.selfcare.ui.login;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.VideoView;

import com.tecnotree.selfcare.App;
import com.tecnotree.selfcare.R;
import com.tecnotree.selfcare.ui.main.MainActivity;


import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity implements LoginScreenContract.View {

    @Inject
    LoginScreenPresenter loginPresenter;

    @BindView(R.id.email)
    EditText usernameInput;

    @BindView(R.id.password)
    EditText passwordInput;

    @BindView(R.id.progressBar2)
    public  ProgressBar progressBar;

    @BindView(R.id.background_video_view)
    VideoView videoView;

     MediaPlayer mMediaPlayer;
    int mCurrentVideoPosition;

    @OnClick(R.id.email_sign_in_button)
    void delegateLogin() {
        if (TextUtils.isEmpty(usernameInput.getText().toString()) && !TextUtils.isEmpty(passwordInput.getText().toString())) {
            usernameInput.setError("Username Required");
        } else if (!TextUtils.isEmpty(usernameInput.getText().toString()) && TextUtils.isEmpty(passwordInput.getText().toString())) {
            passwordInput.setError("Password Required");
        } else if (TextUtils.isEmpty(usernameInput.getText().toString()) && TextUtils.isEmpty(passwordInput.getText().toString())) {
            usernameInput.setError("Username Required");
            passwordInput.setError("Password Required");
        } else if (!TextUtils.isEmpty(usernameInput.getText().toString()) && !TextUtils.isEmpty(passwordInput.getText().toString())) {
            progressBar.setVisibility(View.VISIBLE);
            loginPresenter.doLogin(usernameInput.getText().toString(), passwordInput.getText().toString());
        }
    }

    @OnClick(R.id.register_now_button)
    void registerNow(){
        loginPresenter.doOpenRegisterScreen(this);
    }

    @OnClick(R.id.forgot_password_button)
    void forgotPassword(){
        loginPresenter.doOpenForgotPasswordScreen(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //String UrlPath = "android.resource://" +getPackageName()+"/"+ R.raw.mt_launch_screen;
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        DaggerLoginScreenComponent.builder()
            .loginScreenModule(new LoginScreenModule(this))
            .appComponent(App.getInstance().getApplicationWideDeps())
            .build()
            .inject(this);


        loginPresenter.initScreen();
    }




    @Override
    public void enableLogin() {

    }

    @Override
    public void showValidationError() {

    }

    @Override
    public void showCommunicationError(String message) {
        Snackbar snackBar = Snackbar.make(passwordInput, message, Snackbar.LENGTH_LONG);
        snackBar.show();
    }

    @Override
    public void setCredentials(String username, String password) {
        usernameInput.setText(username);
        passwordInput.setText(password);
        if(username != "" & password !="" ){
            View someView = findViewById(R.id.email_sign_in_button);
            someView.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.sc_card_rounded_sign_in));
        }
        usernameInput.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
            }
            public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
                View someView = findViewById(R.id.email_sign_in_button);
                if (!TextUtils.isEmpty(usernameInput.getText().toString()) && !TextUtils.isEmpty(passwordInput.getText().toString())) {
                    someView.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.sc_card_rounded_sign_in));
                }else {
                    someView.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.sc_card_rounded_transparent_sign_in));
                }
            }
            public void afterTextChanged(final Editable s) {
            }
        });
        passwordInput.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {

            }
            public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
                View someView = findViewById(R.id.email_sign_in_button);
                if (!TextUtils.isEmpty(usernameInput.getText().toString()) && !TextUtils.isEmpty(passwordInput.getText().toString())) {
                    someView.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.sc_card_rounded_sign_in));
                }else {
                    someView.setBackground(ContextCompat.getDrawable(getApplicationContext(), R.drawable.sc_card_rounded_transparent_sign_in));
                }
            }
            public void afterTextChanged(final Editable s) {

            }
        });
    }
}

Simple work.简单的工作。 You can try with app:passwordToggleEnabled="true" .您可以尝试使用app:passwordToggleEnabled="true"

When enabled, a button is placed at the end of the EditText which enables the user to switch between the field's input being visibly disguised or not.启用后,EditText 的末尾会放置一个按钮,使用户能够在字段输入是否被明显伪装之间切换。

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

                    >

                    <androidx.appcompat.widget.AppCompatEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:cursorVisible="true"
                        android:focusable="true"
                        android:inputType="textPassword"
                        android:maxLength="10"
                        android:maxLines="1"
                         />

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

Xml Xml

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/text_input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:errorEnabled="true"
                app:passwordToggleEnabled="true">

                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Password"
                    android:inputType="textPassword"
                    android:backgroundTint="@color/colorPrimary"
                    android:textColor="@color/colorPrimaryDark"/>

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

java java

private TextInputLayout textInputPassword;
 textInputPassword = findViewById(R.id.text_input_login_password);

 // get data from textInputPassword
 String password = textInputPassword.getEditText().getText().toString();

Just use the TextInputLayout included in Material Components library只需使用Material Components 库中包含的TextInputLayout

<com.google.android.material.textfield.TextInputLayout
    ...
    android:hint="Password"
    app:endIconMode="password_toggle">

    <com.google.android.material.textfield.TextInputEditText
        ...
        android:inputType="textPassword"
    />

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM