2010-09-10 103 views
95

之间是否有一个聪明的办法让隐藏和查看密码的用户切换在Android的EditText切换? 许多基于PC的应用程序都让用户执行此操作。如何隐藏和查看密码

回答

88

您可以动态更改TextView的属性。如果您将XML Atrribute android:password设置为true,则该视图将显示点,如果将其设置为false,则显示文本。

随着setTransformationMethod你应该能够改变从代码这个属性的方法。 (免责声明:。如果方法仍然工作时显示的视图后,如果遇到问题,这给我留下了评论,我知道我没有测试过)

完整的示例代码将

yourTextView.setTransformationMethod(new PasswordTransformationMethod()); 

隐藏密码。要显示密码,您可以设置一个现有的转换方法,或者实现一个空的TransformationMethod,它不会对输入文本产生任何影响。

yourTextView.setTransformationMethod(new DoNothingTransformation()); 
+31

要显示密码,您不需要创建任何新类。只需调用'setTransformationMethod(null)'。 – 2014-07-25 01:06:48

+2

@Janusz,下面的使用会给gud解决方案。setTransformationMethod(PasswordTransformationMethod.getInstance());和setTransformationMethod(HideReturnsTransformationMethod.getInstance()); – 2015-04-13 13:31:40

+0

@Janusz但如何让键盘显示/隐藏键? – NarendraJi 2015-08-19 06:04:17

2

你试过setTransformationMethod吗?它是从TextView继承的,并且需要一个TransformationMethod作为参数。

你可以找到更多关于TransformationMethods here

它也有一些很酷的功能,如字符替换。

+0

在回答链接是死 - * “的状态码:404未找到” *。 – Pang 2018-02-14 07:19:45

89

要显示的点,而不是密码的设置PasswordTransformationMethod:

yourEditText.setTransformationMethod(new PasswordTransformationMethod()); 
当然

缺省情况下可以在XML布局你的EditText元素设置此

android:password 

要重新显示读取密码,只要传递null作为变换法:

yourEditText.setTransformationMethod(null); 
+7

'android:password'现已弃用,您应该使用'android:inputType'代替 – Wilka 2011-07-10 17:28:17

+40

您也可以通过在设置转换方法之前保存光标位置并在其之后进行恢复来使用户满意,使用'editText.getSelectionStart )'和'editText.getSelectionEnd()'用于保存光标位置,'editText.setSelection(start,end)'用于恢复它。 – Mostafa 2011-09-07 11:21:14

+1

@Wilka:android:inputType不允许您在运行时在两个状态之间来回切换 - 它只允许你切换一次,一旦你改变它,你不能改变它,并且不,setTransformationMethod不会被废弃。 – AndroidDev 2013-06-30 13:20:27

63

要显示:

editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 

要隐藏:

editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 

每个这些以后,光标被复位,所以:

editText.setSelection(editText.length()); 
+0

在Android 4.3和5.0上测试,效果很棒! Docs使它看起来像应该一直工作到API 3。 – James 2014-12-30 13:14:53

+0

@MattLogan但如何获得键盘显示/隐藏键? – NarendraJi 2015-08-19 06:05:32

+4

这应该是公认的答案 – 2015-08-28 08:11:41

9

使用复选框并相应地改变输入类型。

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    int start,end; 
    Log.i("inside checkbox chnge",""+isChecked); 
    if(!isChecked){ 
     start=passWordEditText.getSelectionStart(); 
     end=passWordEditText.getSelectionEnd(); 
     passWordEditText.setTransformationMethod(new PasswordTransformationMethod());; 
     passWordEditText.setSelection(start,end); 
    }else{ 
     start=passWordEditText.getSelectionStart(); 
     end=passWordEditText.getSelectionEnd(); 
     passWordEditText.setTransformationMethod(null); 
     passWordEditText.setSelection(start,end); 
    } 
} 
+0

工作就像一个魅力谢谢:) – 2015-08-11 13:03:05

3

这对我很有用。这将帮助你肯定

showpass.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        if(!isChecked){ 

        // show password 
        password_login.setTransformationMethod(PasswordTransformationMethod.getInstance()); 

        Log.i("checker", "true"); 
       } 

       else{ 
        Log.i("checker", "false"); 

        // hide password 
    password_login.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); 
       } 

      } 
     }); 
4

我觉得我要回答这个问题,甚至有一些很好的答案,

根据文档TransformationMethod做我们的使命

TransformationMethod

TextView使用TransformationMethods来执行诸如替换之类的操作210个带点的密码字符,或保持换行符 在单行文本字段中不产生换行符。

注意我用黄油刀,但其相同的,如果用户检查显示密码

@OnCheckedChanged(R.id.showpass) 
    public void onChecked(boolean checked){ 
     if(checked){ 
      et_password.setTransformationMethod(null); 
     }else { 
      et_password.setTransformationMethod(new PasswordTransformationMethod()); 

     } 
     // cursor reset his position so we need set position to the end of text 
     et_password.setSelection(et_password.getText().length()); 
    } 
4

我能够用短短几行添加ShowPassword/HidePassword代码,自足在一个区块:

protected void onCreate(Bundle savedInstanceState) { 
    ... 
    etPassword = (EditText)findViewById(R.id.password); 
    etPassword.setTransformationMethod(new PasswordTransformationMethod()); // Hide password initially 

    checkBoxShowPwd = (CheckBox)findViewById(R.id.checkBoxShowPwd); 
    checkBoxShowPwd.setText(getString(R.string.label_show_password)); // Hide initially, but prompting "Show Password" 
    checkBoxShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton arg0, boolean isChecked) { 
      if (isChecked) { 
       etPassword.setTransformationMethod(null); // Show password when box checked 
       checkBoxShowPwd.setText(getString(R.string.label_hide_password)); // Prompting "Hide Password" 
      } else { 
       etPassword.setTransformationMethod(new PasswordTransformationMethod()); // Hide password when box not checked 
       checkBoxShowPwd.setText(getString(R.string.label_show_password)); // Prompting "Show Password" 
      } 
     } 
    }); 
    ... 
2

尝试https://github.com/maksim88/PasswordEditText项目在github。 你甚至不需要使用它来改变你的Java代码。只要改变

的EditText

标签

com.maksim88.passwordedittext.PasswordEditText

在XML文件中。

+0

你有任何想法如何使用setError这个组件,一旦错误被标记显示/隐藏图标变得不可见 – guisantogui 2016-02-18 19:53:38

149

自支持库v24.2.0以来,实现起来非常简单。

您需要做的仅仅是:

  1. 结合添加设计库添加到依赖条件

    dependencies { 
        compile "com.android.support:design:24.2.0" 
    } 
    
  2. 使用TextInputEditTextTextInputLayout

    <android.support.design.widget.TextInputLayout 
        android:id="@+id/etPasswordLayout" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        app:passwordToggleEnabled="true" 
        android:layout_marginBottom="@dimen/login_spacing_bottom"> 
    
        <android.support.design.widget.TextInputEditText 
         android:id="@+id/etPassword" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:hint="@string/fragment_login_password_hint" 
         android:inputType="textPassword"/> 
    </android.support.design.widget.TextInputLayout> 
    

passwordToggleEnabled属性将完成这项工作!

  • 在你的根布局,不要忘记使用添加xmlns:app="http://schemas.android.com/apk/res-auto"

  • 您可以自定义密码切换:

  • app:passwordToggleDrawable - 可绘制到用作密码输入可见性切换图标。
    app:passwordToggleTint - 用于密码输入可见性切换的图标。
    app:passwordToggleTintMode - 用于应用背景色调的混合模式。

    更多详细信息在TextInputLayout documentation

    enter image description here

    +0

    在版本25.1.0我有一个奇怪的行为:它显示密码切换一次,但如果你按下,它会消失o_O' – MiguelHincapieC 2017-01-02 21:38:17

    +1

    是的,也有一些''TextInputEditText'上的'android:text'属性的怪癖。您可以随时在[Android版Google问题跟踪器](https://code.google.com/p/android/issues/list)上提出问题 – mmBs 2017-01-03 08:48:37

    +0

    有什么方法可以将图标移动到左侧? – 2017-01-12 12:44:45

    1

    我所做的就是

    1. 创建一个编辑文本视图和一个普通的文本视图
    2. 请使用约束布局(就像Facebook应用程序登录屏幕它们相互重叠)
    3. 将onClickListener附加到普通文本视图,以便相应地更改编辑文本视图的输入类型(可见/不可见)

    您可以为一个更详细的步骤和解释,看看这个视频https://youtu.be/md3eVaRzdIM

    希望它能帮助:)

    2
    private int passwordNotVisible=1; 
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
    showPassword = (ImageView) findViewById(R.id.show_password); 
        showPassword.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          EditText paswword = (EditText) findViewById(R.id.Password); 
          if (passwordNotVisible == 1) { 
           paswword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); 
           passwordNotVisible = 0; 
          } else { 
    
           paswword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
           passwordNotVisible = 1; 
          } 
    
    
          paswword.setSelection(paswword.length()); 
    
         } 
        }); 
    } 
    
    3

    您可以使用此下面的代码显示/隐藏密码:

    XML代码:

    <EditText 
         android:id="@+id/etPassword" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginLeft="21dp" 
         android:layout_marginTop="14dp" 
         android:ems="10" 
         android:inputType="textPassword" > 
         <requestFocus /> 
        </EditText> 
        <CheckBox 
         android:id="@+id/cbShowPwd" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_alignLeft="@+id/etPassword" 
         android:layout_below="@+id/etPassword" 
         android:text="@string/show_pwd" /> 
    

    Java代码:

    EditText mEtPwd; 
    CheckBox mCbShowPwd; 
    
    
    mEtPwd = (EditText) findViewById(R.id.etPassword); 
    mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd); 
    
    mCbShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
        @Override 
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
         // checkbox status is changed from uncheck to checked. 
         if (!isChecked) { 
          // show password 
          mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
         } else { 
          // hide password 
          mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); 
         } 
        } 
    }); 
    
    -1
    if (inputPassword.getTransformationMethod() == PasswordTransformationMethod.getInstance()) { 
    //password is visible 
           inputPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); 
          } 
    else if(inputPassword.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) { 
    //password is hidden 
           inputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance()); 
          } 
    
    7

    您可以使用应用程序:passwordToggleEnabled = “真”

    这里例如下面

    <android.support.design.widget.TextInputLayout 
         android:id="@+id/password" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         app:passwordToggleEnabled="true" 
         android:textColorHint="@color/colorhint" 
         android:textColor="@color/colortext"> 
    
    0

    这里给出的是我的解决方案,而无需使用TextInputEditText和转换方法。

    XML

    <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:orientation="vertical"> 
    
         <TextView 
          style="@style/FormLabel" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:text="@string/username" /> 
    
         <EditText 
          android:id="@+id/loginUsername" 
          style="@style/EditTextStyle" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:drawableLeft="@drawable/ic_person_outline_black_24dp" 
          android:drawableStart="@drawable/ic_person_outline_black_24dp" 
          android:inputType="textEmailAddress" 
          android:textColor="@color/black" /> 
    
         <TextView 
          style="@style/FormLabel" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_marginTop="20dp" 
          android:text="@string/password" /> 
    
         <EditText 
          android:id="@+id/loginPassword" 
          style="@style/EditTextStyle" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:drawableEnd="@drawable/ic_visibility_off_black_24dp" 
          android:drawableLeft="@drawable/ic_lock_outline_black_24dp" 
          android:drawableRight="@drawable/ic_visibility_off_black_24dp" 
          android:drawableStart="@drawable/ic_lock_outline_black_24dp" 
          android:inputType="textPassword" 
          android:textColor="@color/black" /> 
        </LinearLayout> 
    

    Java代码的

    boolean VISIBLE_PASSWORD = false; //declare as global variable befor onCreate() 
    loginPassword = (EditText)findViewById(R.id.loginPassword); 
    loginPassword.setOnTouchListener(new View.OnTouchListener() { 
         public boolean onTouch(View v, MotionEvent event) { 
          final int DRAWABLE_LEFT = 0; 
          final int DRAWABLE_TOP = 1; 
          final int DRAWABLE_RIGHT = 2; 
          final int DRAWABLE_BOTTOM = 3; 
    
          if (event.getAction() == MotionEvent.ACTION_UP) { 
           if (event.getRawX() >= (loginPassword.getRight() - loginPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { 
            // your action here 
            //Helper.toast(LoginActivity.this, "Toggle visibility"); 
            if (VISIBLE_PASSWORD) { 
             VISIBLE_PASSWORD = false; 
             loginPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); 
             loginPassword.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_outline_black_24dp, 0, R.drawable.ic_visibility_off_black_24dp, 0); 
            } else { 
             VISIBLE_PASSWORD = true; 
             loginPassword.setInputType(InputType.TYPE_CLASS_TEXT); 
             loginPassword.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_outline_black_24dp, 0, R.drawable.ic_visibility_black_24dp, 0); 
            } 
            return false; 
           } 
          } 
          return false; 
         } 
        }); 
    
    0

    编译 'com.android.support:appcompat-v7:24.2.0'

    编译“ com.android.support:design:24.2.0'

    布局

    android:inputType="textPassword" 
    

    其工作