之間是否有一個聰明的辦法讓隱藏和查看密碼的用戶切換在Android的EditText切換? 許多基於PC的應用程序都讓用戶執行此操作。如何隱藏和查看密碼
回答
您可以動態更改TextView的屬性。如果您將XML Atrribute android:password
設置爲true,則該視圖將顯示點,如果將其設置爲false,則顯示文本。
隨着setTransformationMethod你應該能夠改變從代碼這個屬性的方法。 (免責聲明:。如果方法仍然工作時顯示的視圖後,如果遇到問題,這給我留下了評論,我知道我沒有測試過)
完整的示例代碼將
yourTextView.setTransformationMethod(new PasswordTransformationMethod());
隱藏密碼。要顯示密碼,您可以設置一個現有的轉換方法,或者實現一個空的TransformationMethod,它不會對輸入文本產生任何影響。
yourTextView.setTransformationMethod(new DoNothingTransformation());
要顯示的點,而不是密碼的設置PasswordTransformationMethod:
yourEditText.setTransformationMethod(new PasswordTransformationMethod());
當然
缺省情況下可以在XML佈局你的EditText元素設置此
android:password
要重新顯示讀取密碼,只要傳遞null作爲變換法:
yourEditText.setTransformationMethod(null);
'android:password'現已棄用,您應該使用'android:inputType'代替 – Wilka 2011-07-10 17:28:17
您也可以通過在設置轉換方法之前保存光標位置並在其之後進行恢復來使用戶滿意,使用'editText.getSelectionStart )'和'editText.getSelectionEnd()'用於保存光標位置,'editText.setSelection(start,end)'用於恢復它。 – Mostafa 2011-09-07 11:21:14
@Wilka:android:inputType不允許您在運行時在兩個狀態之間來回切換 - 它只允許你切換一次,一旦你改變它,你不能改變它,並且不,setTransformationMethod不會被廢棄。 – AndroidDev 2013-06-30 13:20:27
要顯示:
editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
要隱藏:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
每個這些以後,光標被複位,所以:
editText.setSelection(editText.length());
在Android 4.3和5.0上測試,效果很棒! Docs使它看起來像應該一直工作到API 3。 – James 2014-12-30 13:14:53
@MattLogan但如何獲得鍵盤顯示/隱藏鍵? – NarendraJi 2015-08-19 06:05:32
這應該是公認的答案 – 2015-08-28 08:11:41
使用複選框並相應地改變輸入類型。
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);
}
}
工作就像一個魅力謝謝:) – 2015-08-11 13:03:05
這對我很有用。這將幫助你肯定
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());
}
}
});
我覺得我要回答這個問題,甚至有一些很好的答案,
根據文檔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());
}
我能夠用短短几行添加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"
}
}
});
...
嘗試https://github.com/maksim88/PasswordEditText項目在github。 你甚至不需要使用它來改變你的Java代碼。只要改變
的EditText
標籤
com.maksim88.passwordedittext.PasswordEditText
在XML文件中。
你有任何想法如何使用setError這個組件,一旦錯誤被標記顯示/隱藏圖標變得不可見 – guisantogui 2016-02-18 19:53:38
自支持庫v24.2.0以來,實現起來非常簡單。
您需要做的僅僅是:
結合添加設計庫添加到依賴條件
dependencies { compile "com.android.support:design:24.2.0" }
使用
TextInputEditText
與TextInputLayout
<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。
在版本25.1.0我有一個奇怪的行爲:它顯示密碼切換一次,但如果你按下,它會消失o_O' – MiguelHincapieC 2017-01-02 21:38:17
是的,也有一些''TextInputEditText'上的'android:text'屬性的怪癖。您可以隨時在[Android版Google問題跟蹤器](https://code.google.com/p/android/issues/list)上提出問題 – mmBs 2017-01-03 08:48:37
有什麼方法可以將圖標移動到左側? – 2017-01-12 12:44:45
我所做的就是
- 創建一個編輯文本視圖和一個普通的文本視圖
- 請使用約束佈局(就像Facebook應用程序登錄屏幕它們相互重疊)
- 將onClickListener附加到普通文本視圖,以便相應地更改編輯文本視圖的輸入類型(可見/不可見)
您可以爲一個更詳細的步驟和解釋,看看這個視頻https://youtu.be/md3eVaRzdIM
希望它能幫助:)
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());
}
});
}
您可以使用此下面的代碼顯示/隱藏密碼:
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());
}
}
});
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());
}
您可以使用應用程序: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">
這裏給出的是我的解決方案,而無需使用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;
}
});
編譯 'com.android.support:appcompat-v7:24.2.0'
編譯「 com.android.support:design:24.2.0'
佈局
android:inputType="textPassword"
其工作
- 1. 如何在ios中隱藏和查看密碼之間切換?
- 2. 如何查看和隱藏表中所有行的密碼
- 3. 隱藏密碼
- 4. 隱藏密碼()
- 5. 隱藏UITextField密碼
- 6. 如何從代碼文檔中隱藏查看源代碼?
- 7. 如何隱藏數據庫密碼?
- 8. 如何用密碼術隱藏javascript?
- 9. 隱藏所有MongoDB查詢的密碼
- 10. 如何查看內置的「隱藏」python?
- 11. Active Directory如何查看隱藏屬性
- 12. Android的隱藏號碼,如密碼
- 13. 隱藏窗口,用戶名和密碼
- 14. 顯示和隱藏密碼字符
- 15. 隱藏/查看編輯查看錶格
- 16. 用0height和0width查看並不隱藏
- 17. HTML點擊查看和隱藏公告
- 18. 如何在網站中隱藏「查看源代碼」選項
- 19. 在android pdf查看器barteksc庫中,如何隱藏頁碼?
- 20. 隱藏密碼字符
- 21. 隱藏模擬的密碼
- 22. 隱藏密碼字段
- 23. 在textField中隱藏密碼「•••••••」
- 24. Vim語法隱藏密碼
- 25. Jetty-ssl.xml密碼隱藏
- 26. 隱藏密碼字符串
- 27. iSeries連接隱藏密碼
- 28. 隱藏MySQL的密碼
- 29. 詹金斯 - 隱藏查看
- 30. 隱藏詳細查看
要顯示密碼,您不需要創建任何新類。只需調用'setTransformationMethod(null)'。 – 2014-07-25 01:06:48
@Janusz,下面的使用會給gud解決方案。setTransformationMethod(PasswordTransformationMethod.getInstance());和setTransformationMethod(HideReturnsTransformationMethod.getInstance()); – 2015-04-13 13:31:40
@Janusz但如何讓鍵盤顯示/隱藏鍵? – NarendraJi 2015-08-19 06:04:17