Android text input box (edittext) displays and hides when you enter a password _android

Source: Internet
Author: User

The code is simple, there is no more nonsense.

Copy Code code as follows:

Package cc.c;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.text.Selection;
Import android.text.Spannable;
Import Android.text.method.HideReturnsTransformationMethod;
Import Android.text.method.PasswordTransformationMethod;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;
/**
* Demo Description:
* Text input box (edittext) toggle password display and hide
*
Resources
* 1 http://developer.android.com/reference/android/text/method/HideReturnsTransformationMethod.html
* 2 http://developer.android.com/reference/android/text/method/PasswordTransformationMethod.html
* Thank you very much
*/
public class Mainactivity extends activity {
Private Button Mswitchbutton;
Private EditText Mpasswordedittext;
Private Boolean ishidden=true;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Init ();
}
private void init () {
mswitchbutton= (Button) Findviewbyid (R.id.button);
mpasswordedittext= (EditText) Findviewbyid (R.id.edittext);
Mswitchbutton.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
if (Ishidden) {
Sets the EditText text to be visible
Mpasswordedittext.settransformationmethod (Hidereturnstransformationmethod.getinstance ());
} else {
Sets the EditText text as hidden
Mpasswordedittext.settransformationmethod (Passwordtransformationmethod.getinstance ());
}
Ishidden =!ishidden;
Mpasswordedittext.postinvalidate ();
Place the EditText cursor at the end after switching
Charsequence charsequence = Mpasswordedittext.gettext ();
if (charsequence instanceof spannable) {
Spannable Spantext = (spannable) charsequence;
Selection.setselection (Spantext, Charsequence.length ());
}

}
});
}

}

Have the same needs of small partners come to the reference bar, you can directly use.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.