密码可见与不可见

本文介绍了一个Android应用中实现密码可见与不可见功能的详细步骤,包括代码实现、布局设计以及解决相关问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中一个小功能,密码可见与不可见来回切换。

效果图:

       

代码:

         

public class PwdActivity extends AppCompatActivity {

    public static void startActivity(Context context){
        Intent intent = new Intent(context, PwdActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd);
        final ClearEditText et_server_code = (ClearEditText) findViewById(R.id.et_server_code);
        CheckBox cb_password = (CheckBox) findViewById(R.id.cb_password);
        cb_password.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.getId() == R.id.cb_password && isChecked){//密码可见
                    et_server_code.setInputType(InputType.TYPE_NUMBER_VARIATION_PASSWORD | InputType.TYPE_CLASS_NUMBER);
                }else {//密码不可见
                    et_server_code.setInputType(InputType.TYPE_CLASS_NUMBER);
                }
                et_server_code.setSelection(et_server_code.getText().toString().length());//将光标移至文字末尾
            }
        });
    }
}

 

布局文件:

       

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_marginTop="10dp"
    android:background="@color/color_ffffff"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="15dp"
        android:text="服务密码"
        android:textColor="@color/color_333333"
        android:textSize="14sp" />

    <com.ps.sevenbannermaster.widget.ClearEditText
        android:id="@+id/et_server_code"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:background="@null"
        android:gravity="center_vertical"
        android:hint="请输入服务密码"
        android:inputType="numberPassword"
        android:paddingRight="10dp"
        android:textColor="@color/color_333333"
        android:textSize="14sp" />

    <CheckBox
        android:id="@+id/cb_password"
        style="@style/CbPasswordTheme"
        android:layout_width="27dp"
        android:layout_height="16dp"
        android:checked="true"
        android:layout_gravity="center"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"/>

</LinearLayout>

  样式style:

          

    <style name="CbPasswordTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@null</item>
        <item name="android:background">@drawable/cb_password</item>

    </style>

 

注意:在给ChexBox设置图片时,刚开始发现无论怎么设置尺寸大小,图片都始终按自己的尺寸大小来显示。也就是说我设置的大小无效。这时候我是这么设置的:

    <style name="CbPasswordTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/cb_password</item>

    </style>

所以,要使用正确的设置方法。另外,不使用样式,可以在代码中直接加上两行代码:

android:background="@drawable/ic_selector_traffic"
android:button="@null"

 

备注:

一、

EditText的setSelection()方法失效

 

原因:对EditText设置了

android:singleLine="true"

 

导致失效。

 

 

二、

今天遇到个情况这个InputFilter不好使了,找了半天原因是冲突在android:inputType="textPassword"这个属性上了,当我的EditText作为密码框使用的时候InputFilter不起作用了,只能用笨办法TextWatcher搞定了.

 

三、

EditText中android:digits失效

显示密码:

binding.libLoginEditPwd.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

隐藏密码:

binding.libLoginEditPwd.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD

问题:竟然可以输入文字、表情、符号等

 

解决:

JAVA中不能使用setInputType改变密码显示及隐藏

而使用以下方法:

显示密码:

binding.libLoginEditPwd.transformationMethod = PasswordTransformationMethod.getInstance()


隐藏密码:

binding.libLoginEditPwd.transformationMethod = HideReturnsTransformationMethod.getInstance()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值