Two ways to display clear text and ciphertext in Android EditText

Source: Internet
Author: User

Layout file
<?XML version= "1.0" encoding= "Utf-8"?><!--two ways to display clear text and ciphertext in Android EditText -<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.why.project.androidcnblogsdemo.MainActivity">    <!--Enter the password area -    <EditTextAndroid:id= "@+id/edt_password"Android:layout_width= "Match_parent"Android:layout_height= "50DP"Android:background= "@drawable/comm_input_selector"Android:hint= "Please enter password"android:textsize= "18SP"android:imeoptions= "Actiongo"Android:inputtype= "Textpassword"Android:text=""Android:textcolor= "#999999"Android:textcolorhint= "#999999"Android:drawableleft= "@drawable/pwd_img"android:drawablepadding= "10DP"Android:layout_margin= "5DP"        />    <!--Show hidden password icon -    <ImageViewAndroid:id= "@+id/img_pwdshow"Android:layout_width= "Wrap_content"Android:layout_height= "50DP"android:src= "@drawable/pwd_hidden"Android:scaletype= "Fitcenter"android:contentdescription= "Control password Clear text display"Android:layout_alignright= "@id/edt_password"Android:layout_aligntop= "@id/edt_password"Android:paddingleft= "10DP"Android:paddingright= "10DP"android:focusable= "true"android:clickable= "true"/></Relativelayout>

Comm_input_selector file

<?xml version= "1.0" encoding= "utf-8"?><!--input box background map--><selector xmlns:android= "/http/ Schemas.android.com/apk/res/android > <!--selected and <!--<item android:state_focused= "true"android:drawable= "@drawable/input_box_focused"/>--<!--disable effect when Windows focus is in front of you--<!--<item Android:st Ate_window_focused= "true" android:state_enabled= "false"android:drawable= "@drawable/input_box_focused"/>-<!--default effect-<!--<item android:drawable= "@drawable/input_bo  X_unfocused "/>-<!--when checked and <item android:state_focused=" true "> <shape Android:shape= "Rectangle" > <!--rounded Corners--<corners android:radius= "3DP"/> & lt;!            --stroke--<stroke android:width= "1DP" android:color= "#1296db"/> <!--padding-- <padding Android:bottom= "5DP"Android:left= "5DP"Android:right= "5DP"Android:top= "5DP"/> <!---<solid android:color= "@android: Color/transparent"/> &lt ;/shape> </item> <!--disable effects Windows focus on the previous note--<item android:state_enabled= "false" Android            Oid:state_window_focused= "true" > <shape android:shape= "Rectangle" > <!--rounded Corners-- <corners android:radius= "3DP"/> <!--stroke--<stroke android:width= "1DP" and Roid:color= "#1296db"/> <!--padding-<padding Android:bottom= "5DP"Android:left= "5DP"Android:right= "5DP"Android:top= "5DP"/> <!---<solid android:color= "@android: Color/transparent"/> &lt ;/shape> </item> <!--default Effect--<item> <shape android:shape= "Rectangle"            ; <!--rounded Corners-<corners android:radius= "3DP"/> <!--strokes--<stroke and Roid:width= "1DP" android:color= "#9f9f9f"/> <!--inner margin-<padding Android:bottom= "5DP"Android:left= "5DP"Android:right= "5DP"Android:top= "5DP"/> <!---<solid android:color= "@android: Color/transparent"/> &lt ;/shape> </item> </selector>
Comm_input_selector

Way One
/**Way One:*/    Private voidshoworhiddenpwd () {if(!showpwd) {Showpwd=true;            Img_pwdshow.setimageresource (r.drawable.pwd_show);  // show password  Edt_password.settransformationmethod (Hidereturnstransformationmethod.getinstance ()); }Else{showpwd=false;            Img_pwdshow.setimageresource (R.drawable.pwd_hidden);  // hide Password  Edt_password.settransformationmethod (Passwordtransformationmethod.getinstance ()); }    }
Way Two
/**Way Two:*/    Private voidShoworhiddenpwdwithinputtype () {if(!showpwd) {Showpwd=true;            Img_pwdshow.setimageresource (r.drawable.pwd_show);  // show password  Edt_password.setinputtype (inputtype.type_text_variation_visible_password); }Else{showpwd=false;            Img_pwdshow.setimageresource (R.drawable.pwd_hidden);  // Hide Password edt_password.setinputtype (inputtype.type_class_text | Inputtype.type_text_variation_password); }    }
How to use
 PackageCom.why.project.androidcnblogsdemo;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.text.InputType;ImportAndroid.text.method.HideReturnsTransformationMethod;ImportAndroid.text.method.PasswordTransformationMethod;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;/*** Two ways to display plaintext and ciphertext in Android EditText*/ Public classMainactivityextendsappcompatactivity {PrivateEditText Edt_password; PrivateImageView img_pwdshow; Private BooleanShowpwd =false;//passwords are not displayed by default@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Initviews ();    Initevents (); }    Private voidinitviews () {Edt_password=(EditText) Findviewbyid (R.id.edt_password); Img_pwdshow=(ImageView) Findviewbyid (r.id.img_pwdshow); }    Private voidinitevents () {Img_pwdshow.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {showorhiddenpwdwithinputtype ();    }        }); }    /**Way One:*/    Private voidshoworhiddenpwd () {if(!showpwd) {Showpwd=true;            Img_pwdshow.setimageresource (r.drawable.pwd_show); //Show PasswordEdt_password.settransformationmethod (Hidereturnstransformationmethod.getinstance ()); }Else{showpwd=false;            Img_pwdshow.setimageresource (R.drawable.pwd_hidden); //Hide PasswordEdt_password.settransformationmethod (Passwordtransformationmethod.getinstance ()); }    }    /**Way Two:*/    Private voidShoworhiddenpwdwithinputtype () {if(!showpwd) {Showpwd=true;            Img_pwdshow.setimageresource (r.drawable.pwd_show); //Show PasswordEdt_password.setinputtype (Inputtype.type_text_variation_visible_password); }Else{showpwd=false;            Img_pwdshow.setimageresource (R.drawable.pwd_hidden); //Hide PasswordEdt_password.setinputtype (Inputtype.type_class_text |Inputtype.type_text_variation_password); }    }}
Resources

Android Basics-Login interface, password hiding and display

Two ways to display plaintext and passwords in Android EditText

Two ways to display clear text and ciphertext in Android EditText

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.