2010-09-10 147 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" 
    

    其工作