Android實現動態(tài)顯示或隱藏密碼輸入框的內容
更新時間:2014年09月04日 11:35:01 投稿:shichen2014
這篇文章主要介紹了Android實現動態(tài)顯示或隱藏密碼輸入框的內容,主要通過設置EditText的setTransformationMethod()方法來實現,需要的朋友可以參考下
本文實例展示了Android實現動態(tài)顯示或隱藏密碼輸入框內容的方法,分享給大家供大家參考之用。具體方法如下:
該功能可通過設置EditText的setTransformationMethod()方法來實現隱藏密碼或者顯示密碼。
示例代碼如下:
private Button mBtnPassword;
private EditText mEtPassword;
private boolean mbDisplayFlg = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEtPassword = (EditText)findViewById(R.id.password);
mBtnPassword = (Button)findViewById(R.id.btnPassword);
mBtnPassword.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("AndroidTest", "mbDisplayFlg = " + mbDisplayFlg);
if (!mbDisplayFlg) {
// display password text, for example "123456"
mEtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
// hide password, display "."
mEtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
mbDisplayFlg = !mbDisplayFlg;
mEtPassword.postInvalidate();
}
});
}
main.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/btnPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密碼" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:password="true" android:textSize="18sp" android:text="123456"> </EditText> </LinearLayout>
希望本文所述對大家的Android程序設計有所幫助。
相關文章
自定義RadioButton和ViewPager實現TabHost帶滑動的頁卡效果
在工作中又很多需求都不是android系統(tǒng)自帶的控件可以達到效果的所以這個時候就要自定義控件來達到效果:使用自定義RadioButton和ViewPager實現TabHost帶滑動的頁卡效果2013-01-01
Kotlin封裝RecyclerView Adapter實例教程
這篇文章主要給大家介紹了關于Kotlin封裝RecyclerView Adapter的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08
詳解Android JetPack之LiveData的工作原理
這篇文章主要介紹了詳解Android JetPack之LiveData的工作原理,幫助大家更好的理解和學習使用Android開發(fā),感興趣的朋友可以了解下2021-03-03

