学习笔记------登录界面密码的显示与隐藏

现在很多app开发都有密码显示和隐藏的功能,刚好我现在也遇到这个问题,现在我把我解决的过程分享给大家。

展示页面:

隐藏密码:

在这里插入图片描述

显示密码:

在这里插入图片描述

activity_login.xml布局文件代码:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:layout_marginRight="30dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@mipmap/account" />

            <EditText
                android:id="@+id/edt_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:background="@null"
                android:hint="Warehouse Operator"
                android:textColor="@color/black"
                android:inputType="text" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:background="#333333" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:layout_marginRight="30dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@mipmap/password" />

            <EditText
                android:id="@+id/edt_password"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_weight="1"
                android:background="@null"
                android:hint="Password"
                android:textColor="@color/black"
                android:inputType="textPassword" />

            <ImageView
                android:id="@+id/img_eye"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:src="@mipmap/eye_close" />

        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:background="#333333" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="15dp"
            android:layout_marginRight="30dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <CheckBox
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/remember" />

            <TextView
                android:id="@+id/textView_forgetPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/forgot_password"
                android:textColor="#333333" />


        </LinearLayout>

    </LinearLayout>

接下来是具体的代码实现。

LoginActivity.java的代码:

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

   //输入框的密码是否是隐藏的
    private boolean isHideFirst = true;
    
	private ImageView mImgEye;
	private EditText mEdtUsername;
	private EditText mEdtPassword;
	
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mEdtUsername = findViewById(R.id.edt_username);
        mEdtPassword = findViewById(R.id.edt_password);
        mImgEye = findViewById(R.id.img_eye);
        mImgEye.setOnClickListener(this);
        }

	    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_eye:
                if (isHideFirst == true) {
                    mImgEye.setImageResource(R.mipmap.eye_open);
                    //密文
                    HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
                    mEdtPassword.setTransformationMethod(method1);
                    isHideFirst = false;
                } else {
                    mImgEye.setImageResource(R.mipmap.eye_close);
                    //密文
                    TransformationMethod method = PasswordTransformationMethod.getInstance();
                    mEdtPassword.setTransformationMethod(method);
                    isHideFirst = true;
                }
                //显示密码时光标位置的要获取到当前位置
                int index = mEdtPassword.getText().toString().length();
                mEdtPassword.setSelection(index);
                break;
        }
    }
}

写完这些代码完成后就可以看到开头展示的效果了。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值