Android implements dynamic display or hiding of the contents of the password entry box
- 2020-06-07 05:14:53
- OfStack
This article shows Android's method of dynamically displaying or hiding the contents of password input box. The specific methods are as follows:
This can be done by setting the setTransformationMethod() method of EditText to hide or display the password.
The sample code is as follows:
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 file is as follows:
<?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=" password " />
<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>
I hope this article has been helpful for your Android programming.