(五十四)常用的EditText密码框设置

1、常见的如下所示的EditText密码框设置的实现

2、其中重要的XML文件中的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/LoginFindPasswordMargin1"
    android:layout_width="fill_parent"
    android:layout_height="47dp"
    android:background="@drawable/rectangle_white_bg" >

    <ImageButton
        android:id="@+id/bt_show_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:background="@drawable/password_hide" />

    <ImageButton
        android:id="@+id/bt_delete_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:layout_toLeftOf="@id/bt_show_password"
        android:background="@drawable/fork_selector_light_dark" />

    <EditText
        android:id="@+id/et_input_password"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@id/bt_delete_password"
        android:background="@null"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#%^&amp;*()_=+;:./&lt;>?[]\\{}|`~"
        android:hint="请输入密码"
        android:imeOptions="actionDone"
        android:inputType="textPassword"
        android:paddingLeft="17dp"
        android:paddingRight="0dp"
        android:textColor="#333333"
        android:textColorHint="#a8a8a8"
        android:textSize="14sp" />

</RelativeLayout>

2、部分重要的可以参考的java代码

2.1 设置是否显示bt_delete_password  ImageButton

private void showBtnDeletePassword() {
        mEtPassword.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (mEtPassword.getText().toString().trim() != null
                        && !mEtPassword.getText().toString().trim().equals("")) {
                    mBtnDeletePassword.setVisibility(View.VISIBLE);
                }
                mBtnDeletePassword.setVisibility(View.INVISIBLE);
                return false;
            }
        });
        mEtPassword.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                // TODO Auto-generated method stub

                if (!TextUtils.isEmpty(s)) {
                    mBtnDeletePassword.setVisibility(View.VISIBLE);
                } else {
                    mBtnDeletePassword.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

        });
    }

2.2 设置是EditText是以正常文本形式显示密码还是以密码形式显示密码

      2.2.1 在onCreate中部分初始化代码

mBtnShowPassword = (ImageButton) findViewById(R.id.bt_show_password);
mBtnShowPassword.setOnClickListener(
this); mBtnShowPassword.setTag(R.drawable.password_hide);

     2.2.2 在bt_show_password中的点击事件代码如下所示:

case R.id.bt_show_password:
            int drawavleId;
            try {
                drawavleId = (Integer) mBtnShowPassword.getTag();
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                drawavleId = 0;
            }
            if (drawavleId == R.drawable.password_hide) {
                mBtnShowPassword
                        .setBackgroundResource(R.drawable.password_look);
                mBtnShowPassword.setTag(R.drawable.password_look);
                // 文本正常显示
                mEtPassword
                        .setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);                
                Editable etable = mEtPassword.getText();
                Selection.setSelection(etable, etable.length());
            } else {
                mBtnShowPassword
                        .setBackgroundResource(R.drawable.password_hide);
                mBtnShowPassword.setTag(R.drawable.password_hide);
                // 文本以密码形式显示
                mEtPassword.setInputType(InputType.TYPE_CLASS_TEXT
                        | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                // 下面两行代码实现: 输入框光标一直在输入文本后面
                Editable etable = mEtPassword.getText();
                Selection.setSelection(etable, etable.length());
            }
            break;

 

转载于:https://www.cnblogs.com/fuyanan/p/4424546.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值