Android中密码输入内容可见性切换

本文介绍了一种在Android应用中实现密码输入框可见性切换的方法。通过使用EditText和ImageView组件结合,实现密码显示与隐藏的功能,并附带了具体的XML布局代码及Activity中对应的设置逻辑。

转自Android中密码输入内容可见性切换 - 水寒 - 优快云博客 http://blog.youkuaiyun.com/dawanganban/article/details/23374187#comments

效果如图所示:

1.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#D1D1D1"
    android:gravity="center"
    tools:context="com.lotus.inputpassworddemo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:background="#FFFFFF"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/ed_psw"
            android:drawableLeft="@mipmap/ic_launcher_round"
            android:drawablePadding="8dp"
            android:hint="密码(0-16位)"
            android:inputType="textPassword"
            android:maxLength="16"
            android:singleLine="true"
            android:background="#00000000"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="50dp" />
        <ImageView
            android:id="@+id/img_psw"
            android:layout_weight="8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_psw_no"/>
    </LinearLayout>


</LinearLayout>

2.activity中:

public class MainActivity extends AppCompatActivity {

    private EditText ed_psw;
    private ImageView img_psw;

    boolean flag = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed_psw = (EditText) this.findViewById(R.id.ed_psw);
        img_psw = (ImageView) this.findViewById(R.id.img_psw);

        img_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!flag) {
                    ed_psw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//密码可见
                    img_psw.setImageResource(R.mipmap.ic_psw_yes);
                } else {
                    ed_psw.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码不可见
                    img_psw.setImageResource(R.mipmap.ic_psw_no);
                }
                flag = !flag;
                ed_psw.postInvalidate();
                //防止每次切换密码可见与不可见后光标会回到行首的情况
                CharSequence text = ed_psw.getText();
                if (text instanceof Spannable) {
                    Spannable spanText = (Spannable)text;
                    Selection.setSelection(spanText, text.length());
                }
            }
        });
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值