(五十四)常用的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;

 

posted @ 2015-04-14 12:23  小菜美妞成长中  阅读(814)  评论(1编辑  收藏  举报