Android Implementation Login Small Demo

Source: Internet
Author: User

Android, in the small part of the years before the internship, the small series have not played, if said to play, that is the Android phone, cough, knocking at the time of a special long-lost familiar, this familiar feeling and at that time Knock Machine room charge system, that call a difficult ah, but small make believe, in small series of it growth on the road, It is because of these difficulties accompanied by small knitting together to grow up, just let the small part more brave and strong, brave face one after another bug, a strong knock after a line of code, after a few days of research login a line of the function has been realized, and now small series to a simple summary, but also small partners to give more advice oh ' (*∩_∩ *)′!

In general, the Android architecture and we have learned before the three-layer architecture is very similar, because the knowledge is the same, hey, so start up a bit more familiar feeling, small series knocking login idea is such, first, in the model inside the establishment of entities, Then in res in the layout of the creation of an XML file, to the required page layout, at this point, small series on the pendulum control has not much feeling, but also need a lot of practice, and finally in the SRC UI inside the creation of a class to write specific logic. At the time of the knock log on the online download a lot of demo, but their architecture and small part of the project is now knocked out of the structure is not very similar, so stones, along the way, small pieces fell black and black, but the process is very beautiful ' (*∩_∩*), Next, we look at the specific code implementation.

First of all, we need to build the model inside the entity, because the small series used to two tables, so need to build two model, the first to use is loginmsg, the code is as follows:

/*** * Description: Information required for login * Ding * time: July 17, 2015 09:40:18 */package jczb.shoping.model;import java.io.serializable;import Android Oid. r.string;//required field information for login public class Loginmsg implements Serializable {private string Avatarpath;private string AccountNum ;p rivate string littlename;private string memberrank;private int growths;private string Memberintegral;public string Getavatarpath () {return avatarpath;} public void Setavatarpath (String avatarpath) {this.avatarpath = Avatarpath;} Public String Getaccountnum () {return accountnum;} public void Setaccountnum (String accountnum) {this.accountnum = AccountNum;} Public String Getlittlename () {return littlename;} public void Setlittlename (String littlename) {this.littlename = Littlename;} Public String Getmemberrank () {return memberrank;} public void Setmemberrank (String memberrank) {this.memberrank = Memberrank;} public int getgrowths () {return growths;} public void setgrowths (int growths) {this.growths = growths;} Public String getmemberintegral () {return MembeRintegral;} public void Setmemberintegral (String memberintegral) {this.memberintegral = memberintegral;}}

The second use of the Foundpassword, the specific code is as follows:

/*** * Description: Find information about password * Ding * time: July 17, 2015 09:41:18 */ Package Jczb.shoping.model;import Java.io.serializable;import Android. R.string;public class Foundpassword implements Serializable {private string PhoneNumber;//Mobile number private string Vlida Tionnum; Verification code Private String PassWord; Password private String Repetypassword; Confirm Password Public String Getphonenumber () {return phonenumber;} public void Setphonenumber (String phonenumber) {this.phonenumber = PhoneNumber;} Public String Getvlidationnum () {return vlidationnum;} public void Setvlidationnum (String vlidationnum) {this.vlidationnum = Vlidationnum;} Public String GetPassword () {return passWord;} public void SetPassword (String passWord) {This.password = PassWord;} Public String Getrepetypassword () {return repetypassword;} public void Setrepetypassword (String repetypassword) {This.repetypassword = Repetypassword;}}  

Next, we draw the page, using XML to set the layout of our login page, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/     Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " android:background= "@color/login_background_color" > <!--the layout of Tesco--<textview Android:lay Out_width= "Match_parent" android:layout_height= "wrap_content" android:layout_margintop= "30DP" Android : gravity= "Center" android:text= "Learning Tesco" android:textcolor= "@color/blue_ipsetting" android:textsize= "30sp                "android:textstyle=" bold "/> <!--the entire linearlayout is the account number and please enter the account of the rectangle frame--and <linearlayout Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" Android:layout_ma Rgin= "5DP" android:orientation= "horizontal" android:padding= "10SP" Android:ba       ckground= "@color/white" >                         <!--the layout of your account--<textview android:id= "@+id/tv_loginacco                    Unt "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "                    android:text= "@string/loginaccount" android:textcolor= "@android: Color/black"                    Android:textsize= "18.0sp"/> <!--EditText means please enter a layout of your account--and <edittext Android:id= "@+id/loginaccount_id" android:layout_width= "160DP" Andro                                         id:layout_height= "Wrap_content" android:layout_weight= "1" android:background= "@null"                    Android:ems= "Ten" android:hint= "@string/inputaccount" android:padding= "5.0dip" android:paddingright= "40DP" an Droid:textcolor= "#ff3b3b3b "android:textsize=" 16.0SP "> </EditText> </LinearLayout> <!--password and enter the entire layout of your password--<linearlayout android:layout_width= "Fill_parent" android:layout _height= "Wrap_content" android:layout_margin= "5DP" android:orientation= "Horizontal" Android:pad                ding= "10SP" android:background= "@color/white" > <!--Password Layout--<textview Android:id= "@+id/tv_password" android:layout_width= "Wrap_content" Android:layout_              height= "Wrap_content" android:text= "@string/password" android:textcolor= "@android: Color/black" Android:textsize= "18.0sp"/> <!--Enter the layout of your password--<edittext a              Ndroid:id= "@+id/password_id" android:layout_width= "163DP" android:layout_height= "Wrap_content" Android:layout_weight= "1" android:background= "@null" android:ems= "android:hint=" @string/INP Utpassword "android:inputtype=" Textpassword "android:padding=" 5.0dip "Android:paddi                             ngright= "30DP" android:textcolor= "#ff3b3b3b" android:textsize= "16.0sp"/> </LinearLayout> <button android:id= "@+id/login" android:layout_width= "f Ill_parent "android:layout_height=" wrap_content "android:layout_marginbottom=" 14DP "Andro            id:layout_marginleft= "15DP" android:layout_marginright= "15DP" android:layout_margintop= "80DP" android:background= "@drawable/android_title_bg" android:gravity= "center" android:text= "@string/ Login "android:textcolor=" #fff "/> <!--Find your password and understand the layout of your registration-<!--find your secret Code and immediately register the layout--&LT LinearLayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:la Yout_margin= "5DP" android:background= "@color/white" android:orientation= "Horizontal" Android:pa              dding= "10SP" > <!--recover password--<textview android:layout_width= "70DP"              android:layout_height= "50DP" android:layout_marginleft= "40DP" android:layout_margintop= "15DP"          android:text= "Recover password" android:textsize= "15sp" android:textstyle= "bold"/> <!--Register now-<textview android:layout_width= "70DP" android:layout_height= "56d P "android:layout_marginleft=" 55DP "android:layout_margintop=" 15DP "Android:layout_ weight= "0.03" android:text= "Register Now" android:textsize= "15SP" android:textstyle= "bold" /> </linearLayout> </LinearLayout>                      
Finally, take a look at the specific logical part of the code, as follows:

Package Jczb.shoping.ui;import Java.security.publickey;import Java.util.hashmap;import java.util.List;import Java.util.map;import Com.alibaba.fastjson.json;import Jczb.shoping.common.agentapi;import Jczb.shoping.model.foundpassword;import Jczb.shoping.model.loginmsg;import Android. R.integer;import Android. R.string;import Android.accounts.account;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;import Android.widget.toast;public class Loginactivity extends Activity implements Onclicklistener {//StatementVariable private Handler Mhandler; EditText account;    EditText password;private Button Loginbutton;        protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);                Setcontentview (R.layout.activity_login);        Account = (EditText) Findviewbyid (r.id.loginaccount_id);        Password= (EditText) Findviewbyid (r.id.password_id);                loginbutton= (Button) Findviewbyid (R.id.login); The login button is set to listen, the method is implemented by the following onclick Loginbutton.setonclicklistener (this);} @Override/** * Implement the login button's jump */public void OnClick (View v) {//based on the ID to determine which control is clicked, fixed notation switch (V.getid ()) {case R.id.login:login () ; break;default:break;}} /** * Login Method */public Boolean login () {if (Isusernameandpwdvalid ()) {mhandler=new Handler () {public void Handlemessage (    Message msg) {switch (msg.what) {case-1: Toast.maketext (loginactivity.this, "Server Connection Failed!", Toast.length_short). Show ();   Break  Case-2: Toast.maketext (Loginactivity.this, "Oops, something went wrong ...", Toast.length_short). Show (); Break   Case 1:string temp= (String) msg.obj;   Will get the JSON converted to an array of list<loginmsg> Loginmsginfo=json.parsearray (temp, loginmsg.class);             List<foundpassword> Foundpasswordinfo=json.parsearray (Temp,foundpassword.class);        String Username=account.gettext (). toString (). Trim ();      String Pwd=password.gettext (). toString (). Trim ();   String accountnum=loginmsginfo.get (0). Getaccountnum ();       String psaaword=foundpasswordinfo.get (0). GetPassword (); if (Account.equals (AccountNum) && pwd.equals (Psaaword)) {//Implement interface jump Intent Intent = new Intent (Loginac           Tivity.this,mainactivity.class);           StartActivity (Intent);      Close the current interface finish ();    }else{//implementation interface of the jump Intent Intent = new Intent (loginactivity.this,mainactivity.class);    StartActivity (Intent);      Close the current interface finish ();//Toast.maketext (Loginactivity.this, "wrong username or password", 0). Show ();                   }     }   }   }; Main thread new Thread () {public void run () {MEssage msg= new Message ();    try{map<string,string> parmas=new hashmap<string,string> ();    Parmas.put ("username", "1"); Parmas.put ("Password", "2"); String loginmsgurl= "Http://192.168.1.110:8080/SchoolShopJson/LoginMsg.txt";            String foundpasswordurl= "Http://192.168.1.110:8080/SchoolShopJson/foundPassWord.txt";                The data to be sent and the address of the Access String Resultloginmsgstring=agentapi.dopost (Parmas,loginmsgurl);            String resultfpasswordstring=agentapi.dopost (Parmas, Foundpasswordurl);    Send handler information msg.what=1;                msg.obj=resultloginmsgstring;    }catch (Exception e) {e.printstacktrace ();    Use 1 to represent program exception msg.what=-2;    Msg.obj=e;    } mhandler.sendmessage (msg); }}.start ();} return false;} /** * Determine if the user name and password are valid * * @return */public boolean isusernameandpwdvalid () {//username and password must not be empty if (Account.gettext (). toString (). tr Im (). Equals ("")) {Toast.maketext (this, getString (r.string.accountname_empty), Toast.length_short). Show (); return FALSE;} else if (Password.gettext (). toString (). Trim (). Equals ("")) {Toast.maketext (this, getString (r.string.password_empty) , Toast.length_short). Show (); return false;} return true;}}
Finally, let's look at the run effect if the user name and password are correctly skipped to the home page, if the username and password are incorrect, as shown below:

Small Series of words: The blog, the small part of the main simple introduction of how to achieve the Android login, but to say the truth, small part to the present or foggy, for the XML layout page, small series is not skilled, but it doesn't matter, small series will be more and more brave, this is the meaning of life, or that sentence For the small part, is not only the challenge is the opportunity, because the knowledge are interlinked, moreover, in the small part of the process of life, leaving the most precious memories, although the later small not necessarily engaged in the Android industry, the code world, a lot of things, some sweet, some warm, some of the euphemism into songs, and some stretches, in these stories The only thing we have in common is that one year, one month, some placid day, I loved you very much! Love You-the days of this internship, Android brought to the small series of various surprises.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Implementation Login Small Demo

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.