Android Login Interface Implementation code sharing _android

Source: Internet
Author: User
Tags gettext visibility

Recently because of the project needs, the baby has not done Android for a long time, but also because the project needs, now continue to make Android, hey, said more than tears ah, other needless to say, first to engage in a login interface to practice practicing, login interface can be said to be the most commonly used Android project If this is not possible, then go straight to the 21st century floor.

Nonsense not to say, first on the effect of the picture, if you feel good, please refer to the implementation code.

Believe this slag layout is too easy for many people, directly on the layout:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:custom= "http:// Schemas.android.com/apk/res-auto "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android: 
Fitssystemwindows= "true" > <relativelayout android:id= "@+id/login_layout" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_marginleft= "20DP" android:layout_marginright= "20DP" Android: gravity= "center" > <framelayout android:id= "@+id/username_layout" android:layout_width= "Fill_parent" Android: layout_height= "Wrap_content" android:layout_margintop= "55DP" android:gravity= "center" > <!--android: Inputtype= "number"--> <edittext android:id= "@+id/username" android:layout_width= "Fill_parent" android:layout _height= "40DP" android:layout_margintop= "5DP" android:maxlength= "android:paddingleft=" 55DP "Android: paddingright= "60DP" > </EditText> <imageview android:layout_width= "22DP" Android:layout_height= "21DP" android:layout_gravity= "left|center_vertical" android:layout_marginstart= "10DP" Android: background= "@drawable/username" android:visibility= "visible"/> <textview android:id= "@+id/contry_sn" Android : layout_width= "40DP" android:layout_height= "50DP" android:layout_gravity= "left|center_vertical" Android:layout_ margintop= "4DP" android:gravity= "center" android:text= "+62" android:textcolor= "@android: Color/black" android: Textsize= "18SP" android:visibility= "invisible"/> <button android:id= "@+id/bt_username_clear" Android:layout_ Width= "35DP" android:layout_height= "35DP" android:layout_gravity= "right|center_vertical" Android:layout_ marginright= "10DP" android:background= "@drawable/email_delete_pressed" android:visibility= "invisible"/> </ framelayout> <framelayout android:id= "@+id/usercode_layout" android:layout_width= "Fill_parent" Android: layout_height= "Wrap_content" android:layout_below= "@id/username_layout" android:layout_margintop= "6DP" android:gravity= "center" > <edittext android:id= "@+id/password" Fill_parent "android:layout_height=" 40DP "android:inputtype=" Textpassword "Android:maxlength=" Android: paddingleft= "55DP" android:paddingright= "60DP" > </EditText> <imageview android:layout_width= "18DP" Andr oid:layout_height= "21DP" android:layout_gravity= "left|center_vertical" android:layout_marginstart= "10DP" Android: background= "@drawable/password"/> <button android:id= "@+id/bt_pwd_eye" android:layout_width= "40DP" Android: layout_height= "40DP" android:layout_gravity= "right|center_vertical" android:layout_marginright= "10DP" Android: 
background= "@drawable/password_close"/> <button android:id= "@+id/bt_pwd_clear" android:layout_width= "35DP" 
android:layout_height= "35DP" android:layout_gravity= "right|center_vertical" android:layout_marginright= "45DP" android:background= "@drawable/email_delete_pressed" android:visibility= "Invisible "/> </FrameLayout> <button android:id=" @+id/login "Android:layout_width=" Android: layout_height= "40DP" android:layout_below= "@id/usercode_layout" android:layout_margintop= "30DP" Android: background= "@drawable/login_selector" android:gravity= "center" android:text= "Login" android:textcolor= "@android: Color/white "/> <button android:id= @+id/forgive_pwd" android:layout_width= "Wrap_content" Android:layout_ height= "Wrap_content" android:layout_alignright= "@id/login" android:layout_below= "@id/login" android:background= " 
#00000000 "android:text=" Forget the password? Android:textcolor= "@drawable/text_color_selector" android:textsize= "16sp"/> <button android:id= "@+id/ Register "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layout_alignleft=" @id 
Login "android:layout_below=" @id/login "android:background=" #00000000 "android:gravity=" left|center_vertical " android:text= "register" android:textcolor= "@drawable/text_cOlor_selector "android:textsize=" 16sp "android:visibility=" visible "/> </RelativeLayout> </  Relativelayout>

The

Mainactivity is as follows:

Package Com.example.logindemo; 
Import android.support.v7.app.ActionBarActivity; 
Import android.text.Editable; 
Import Android.text.TextWatcher; 
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; 
Import Android.widget.Toast; 
Import Android.os.Bundle;  
/** * Login Interface Demo * * * @author zhy * */public class Mainactivity extends Actionbaractivity implements Onclicklistener { 
Private edittext username, password; 
Private Button bt_username_clear; 
Private Button bt_pwd_clear; 
Private Button forgive_pwd; 
Private Button Bt_pwd_eye; 
Private Button login; 
Private Button Register; 
Private Boolean isOpen = false; 
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.activity_main); 
Initview (); } private void InitView () {username = (edittext) Findviewbyid (r.id.username); Listening for text box content changes Username.addtextchangedlistener (new Textwatcher () {@Override public void ontextchanged (Charsequence s, 
t start, int before, int count) {//Get the user in the text box String user = Username.gettext (). toString (). Trim (); 
if ("" ". Equals (user)) {//user name is empty, the setting button is not visible bt_username_clear.setvisibility (view.invisible); 
} else {//user name is not empty, set button visible bt_username_clear.setvisibility (view.visible); @Override public void beforetextchanged (charsequence s, int start, int count, int in) {} @Override public V 
OID aftertextchanged (Editable s) {}}); 
Password = (edittext) Findviewbyid (R.id.password); Listening for text box content changes Password.addtextchangedlistener (new Textwatcher () {@Override public void ontextchanged (Charsequence s, 
t start, int before, int count) {//Get the user String pwd = password.gettext () in the text box. ToString (). Trim (); 
if ("" ". Equals (PWD)) {//user name is empty, the Settings button is not visible bt_pwd_clear.setvisibility (view.invisible); } else {//user name does notis empty, the setting button is visible bt_pwd_clear.setvisibility (view.visible); @Override public void beforetextchanged (charsequence s, int start, int count, int in) {} @Override public V 
OID aftertextchanged (Editable s) {}}); 
Bt_username_clear = (Button) Findviewbyid (r.id.bt_username_clear); 
Bt_username_clear.setonclicklistener (this); 
Bt_pwd_clear = (Button) Findviewbyid (r.id.bt_pwd_clear); 

Bt_pwd_clear.setonclicklistener (this); 
Bt_pwd_eye = (Button) Findviewbyid (R.id.bt_pwd_eye); 
Bt_pwd_eye.setonclicklistener (this); 
Login = (Button) Findviewbyid (R.id.login); 
Login.setonclicklistener (this); 
Egister = (Button) Findviewbyid (r.id.register); 
Register.setonclicklistener (this); 
Forgive_pwd = (Button) Findviewbyid (R.ID.FORGIVE_PWD); 
Forgive_pwd.setonclicklistener (this); @Override public void OnClick (View v) {switch (V.getid ()) {case R.id.bt_username_clear://Clear Logon name Username.settex 
T (""); 
Break 
Case R.id.bt_pwd_clear://Clear password Password.settext (""); 
Break Case R.id.bt_pwd_eye://password visible and invisible toggle if (isOpen) {isOpen = false; 
else {IsOpen = true; 
}//Default isOpen is False, password is not visible changepwdopenorclose (IsOpen); 
Break 
Case R.id.login://TODO login button break; 
Case R.id.register://Registration button Toast.maketext (mainactivity.this, "registered", 0). Show (); 
Break 
Case R.ID.FORGIVE_PWD://Forgot Password button toast.maketext (mainactivity.this, "Forgot password", 0). Show (); 
Break 
Default:break; }/** * Password visible and invisible toggle * * @param flag/private void Changepwdopenorclose (Boolean flag) {//The first time over is false, the password is not visible I 
F (flag) {//Password visible bt_pwd_eye.setbackgroundresource (r.drawable.password_open); 
Set EditText password visible password.settransformationmethod (hidereturnstransformationmethod. getinstance ()); 
else {//password does not receive Bt_pwd_eye.setbackgroundresource (R.drawable.password_close); 
Sets the EditText password-hiding Password.settransformationmethod (Passwordtransformationmethod. getinstance ());  } 
} 
}

Ok, that is so simple, the effect is complete.

The above is a small set to introduce the Android login interface code to share, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.