zoukankan      html  css  js  c++  java
  • EditText显示明文与密码

    布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        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:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="liu.basedemo.MainActivity">
    
    
        <EditText
            android:id="@+id/etUsername"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:hint="请输入用户名"
            android:textColor="#000000"
            android:textColorHint="#55000000"
            android:textSize="20sp"/>
    
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">
    
            <EditText
                android:id="@+id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20dp"
                android:hint="请输入密码"
                android:inputType="textPassword"
                android:textColor="#000000"
                android:textColorHint="#55000000"
                android:textSize="20sp"/>
    
            <CheckBox
                android:checked="false"
                android:id="@+id/cbDisplayPassword"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:button="@drawable/selector_password"/>
        </RelativeLayout>
    
    </LinearLayout>

    selector

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

    第一种方式

     private void initListener() {
    
            mCbDisplayPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
                    Log.d(TAG, "onCheckedChanged: "+isChecked);
                    if(isChecked){
                        //选择状态 显示明文--设置为可见的密码
    
                        mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    }else {
                        //默认状态显示密码--设置文本 要一起写才能起作用  InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
    
                        mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    }
                }
            });
    
        }

    第二种方式

    private void initListener() {
    
            mCbDisplayPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
                    Log.d(TAG, "onCheckedChanged: "+isChecked);
                    if(isChecked){
                        //选择状态 显示明文--设置为可见的密码
    
                        //mEtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
    
                        /**
                         * 第二种
                         */
                        mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    
                    }else {
                        //默认状态显示密码--设置文本 要一起写才能起作用  InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
    
                        //mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    
                        /**
                         * 第二种
                         */
                        mEtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    }
                }
            });
    
        }
  • 相关阅读:
    【Manacher(马拉车)算法】
    【可持久化数据结构】
    react-dva修改默认端口的方法
    关于React Hooks使用
    react hook useContext 跨文件接收
    el-select中使用el-tooltip时,在下拉滚动时整个网页会出现滚动条,并抖动
    react的onClick执行函数和bind(this)问题
    禁用h5页面中长按图片弹出的弹层
    JS树结构操作:查找、遍历、筛选、树结构和列表结构相互转换
    js树结构查找节点
  • 原文地址:https://www.cnblogs.com/liunanjava/p/5744088.html
Copyright © 2011-2022 走看看