1, the common example of the following edittext the implementation of the Password box settings
2. The code in an important XML file
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"style= "@style/loginfindpasswordmargin1"Android:layout_width= "Fill_parent"Android:layout_height= "47DP"Android:background= "@drawable/rectangle_white_bg" > <ImageButtonAndroid:id= "@+id/bt_show_password"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparentright= "true"android:layout_centervertical= "true"Android:layout_marginright= "10DP"Android:background= "@drawable/password_hide" /> <ImageButtonAndroid:id= "@+id/bt_delete_password"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centervertical= "true"Android:layout_marginright= "10DP"Android:layout_toleftof= "@id/bt_show_password"Android:background= "@drawable/fork_selector_light_dark" /> <EditTextAndroid:id= "@+id/et_input_password"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:layout_marginright= "5DP"Android:layout_toleftof= "@id/bt_delete_password"Android:background= "@null"android:digits= "[Email protected]#%^&* () _=+;:./<>? []\\{}|`~"Android:hint= "Please enter password"android:imeoptions= "Actiondone"Android:inputtype= "Textpassword"Android:paddingleft= "17DP"Android:paddingright= "0DP"Android:textcolor= "#333333"Android:textcolorhint= "#a8a8a8"android:textsize= "14SP" /></Relativelayout>
2. Some important Java code that can be referenced
2.1 Set whether to display Bt_delete_password ImageButton
Private voidShowbtndeletepassword () {Metpassword.setontouchlistener (NewOntouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {//TODO auto-generated Method Stub if(Metpassword.gettext (). toString (). Trim ()! =NULL&&!metpassword.gettext (). toString (). Trim (). Equals ("") {mbtndeletepassword.setvisibility (view.visible); } mbtndeletepassword.setvisibility (view.invisible); return false; } }); Metpassword.addtextchangedlistener (NewTextwatcher () {@Override Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) { //TODO auto-generated Method Stub} @Override Public voidOnTextChanged (Charsequence S,intStartintbefore,intcount) { //TODO auto-generated Method Stub if(!Textutils.isempty (s)) {mbtndeletepassword.setvisibility (view.visible); } Else{mbtndeletepassword.setvisibility (view.invisible); }} @Override Public voidaftertextchanged (Editable s) {//TODO auto-generated Method Stub } }); }
2.2 Set whether EditText displays the password as normal text or as a password
2.2.1 Partial initialization of code in OnCreate
Mbtnshowpassword = (ImageButton) Findviewbyid (R.id.bt_show_password);
Mbtnshowpassword.setonclicklistener (this); Mbtnshowpassword.settag (r.drawable.password_hide);
The Click event code for 2.2.2 in Bt_show_password is as follows:
CaseR.id.bt_show_password:intDrawavleid; Try{Drawavleid=(Integer) Mbtnshowpassword.gettag (); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); Drawavleid= 0; } if(Drawavleid = =r.drawable.password_hide) {Mbtnshowpassword. Setbackgroundresource (R.drawable.password_look); Mbtnshowpassword.settag (R.drawable.password_look); //text is displayed normallyMetpassword. Setinputtype (Inputtype.type_text_variation_visible_password); Editable etable=Metpassword.gettext (); Selection.setselection (Etable, Etable.length ()); } Else{Mbtnshowpassword. Setbackgroundresource (R.drawable.password_hide); Mbtnshowpassword.settag (R.drawable.password_hide); //text is displayed in a password formatMetpassword.setinputtype (Inputtype.type_class_text|Inputtype.type_text_variation_password); //the following two lines of code are implemented: the input box cursor is always behind the input textEditable etable =Metpassword.gettext (); Selection.setselection (Etable, Etable.length ()); } Break;
(54) Common EditText Password box settings