zoukankan      html  css  js  c++  java
  • (五十四)常用的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;
  • 相关阅读:
    高效能人士懂得忽视,知道怎样说“不”
    CheckBoxPreference组件
    SQL基础--&gt; 约束(CONSTRAINT)
    html5--6-7 CSS选择器4
    html5--6-6 CSS选择器3
    html5--6-5 CSS选择器2
    html5--6-4 CSS选择器
    html5--6-3 CSS语法2
    html5--6-2 CSS语法
    html5--6-1 引入外部样式表
  • 原文地址:https://www.cnblogs.com/fuyanan/p/4424546.html
Copyright © 2011-2022 走看看