Android logon demo and android logon demo

Source: Internet
Author: User

Android logon demo and android logon demo

Android, during the years before the mini-editor's internship, I have never played any of them. If I have played some games, I am familiar with the Android mobile phone, cough, and login, this familiar feeling is the same as knocking on the data center charging system at the time. IT is difficult, but I believe that on the way to IT development of xiaobian, it is precisely because of these difficulties and obstacles that accompany the growth of small editors that make small editors more brave and strong, brave to face one bug after another, and strong to finish a line of code, after a few days of research, the function of logging on to a line has been implemented. Now let's make a brief summary of it. Please also ask your friends to give me some advice '(* login _ login *)′!

In general, the Android architecture is very similar to the three-tier architecture we have learned before, because the knowledge is the same. Hey hey, I am a little more familiar with it, the idea of logging on is as follows: first, create an object in the model, and then create an XML file in layout in res to layout the required page. So far, I don't feel much about the Pendulum Control yet. I still need to do more exercises. Finally, I created a class to write the specific logic in the src ui. While logging on, xiaobian downloaded a lot of demos on the Internet, but their architecture is not very similar to the architecture of the project currently being knocked on by xiaobian, so they just crossed the river and walked all the way, small make up has a bad face, but the process is very beautiful '(* ← _ ← *)', next, let's look at the specific code implementation.

First, we need to create an object in the model. Because the small Editor uses two tables, we need to create two models. The first one is LoginMsg. The Code is as follows:

/***** Description: related information required for Logon * Author: Ding Guohua * Time: 09:40:18, January 1, July 17, 2015 */package jczb. shoping. model; import java. io. serializable; import android. r. string; // field information required for Logon public class loginMsg implements Serializable {private String avatarPath; private String accountNum; private 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 FoundPassWord is used. The Code is as follows:

/***** Description: password retrieval information * Author: Ding Guohua * Time: 09:41:18, December 3, July 17, 2015 */package jczb. shoping. model; import java. io. serializable; import android. r. string; public class foundPassWord implements Serializable {private String phoneNumber; // mobile phone number private String vlidationNum; // Verification Code private String passWord; // passWord private String repetyPassWord; // confirm the 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, let's draw a page and use xml to set the layout of our login page. The specific 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"> <! -- Layout of Tesco --> <TextView android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_marginTop = "30dp" android: gravity = "center" android: text = "" android: textColor = "@ color/blue_ipsetting" android: textSize = "30sp" android: textStyle = "bold"/> <! -- The entire LinearLayout is the account and the rectangular frame of the account. --> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_margin = "5dp" android: orientation = "horizontal" android: padding = "10sp" android: background = "@ color/white"> <! -- Account layout --> <TextView android: id = "@ + id/TV _loginAccount" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "@ string/loginAccount" android: textColor = "@ android: color/black" android: textSize = "18366sp"/> <! -- EditText indicates entering an account layout --> <EditText android: id = "@ + id/loginAccount_id" android: layout_width = "160dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: background = "@ null" android: EMS = "10" android: hint = "@ string/inputAccount" android: padding = "5.0dip" android: paddingRight = "40dp" android: textColor = "# ff3b3b3b" android: textSize = "16366sp"> </EditText> </LinearLayout> <! -- Enter the entire layout of the password and password --> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_margin = "5dp" android: orientation = "horizontal" android: padding = "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 = "18366sp"/> <! -- Enter the password layout --> <EditText android: id = "@ + id/password_id" android: layout_width = "163dp" android: layout_height = "wrap_content" android: layout_weight = "1" android: background = "@ null" android: EMS = "10" android: hint = "@ string/inputPassword" android: inputType = "textPassword" android: padding = "5.0dip" android: paddingRight = "30dp" android: textColor = "# ff3b3b3b" android: textSize = "16366sp"/> </LinearLayout> <Button android Oid: id = "@ + id/login" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_marginBottom = "14dp" android: 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"/> <! -- Retrieve the password and understand the registration layout --> <! -- Retrieve the password and register now layout --> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_margin = "5dp" android: background = "@ color/white" android: orientation = "horizontal" android: padding = "10sp"> <! -- Retrieve password --> <TextView android: layout_width = "70dp" android: layout_height = "50dp" android: layout_marginLeft = "40dp" android: layout_marginTop = "15dp" android: text = "retrieve password" android: textSize = "15sp" android: textStyle = "bold"/> <! -- Register now --> <TextView android: layout_width = "70dp" android: layout_height = "56dp" android: layout_marginLeft = "55dp" android: layout_marginTop = "15dp" android: layout_weight = "0.03" android: text = "register now" android: textSize = "15sp" android: textStyle = "bold"/> </LinearLayout>
Finally, let's take a look at the specific logic part of the Code. The specific code is 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. in Tent; 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 {// declare the variable private Handler mHandler; EditText account; EditText pass Word; 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); // set a listener for the logon button, which is implemented by Onclick. setOnClickListener (this);} @ Override /*** Jump to the login button */public void onClick (View v) {// determine which control is clicked Based on the id, fixed statement switch (v. getId () {case R. id. login: login (); break; default: break;}/*** logon 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, "oh, an error... ", Toast. LENGTH_SHORT ). show (); break; case 1: String temp = (String) msg. obj; // convert the obtained json to an array 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) {// jump to Intent intent = new Intent (LoginActivity. this, MainActivity. class); startActivity (intent); // close the current interface finish ();} else {// jump to the implementation interface Intent intent = new Intent (LoginActivity. this, MainActivity. class); startActivity (intent); // close the current interface to finish (); // Toast. makeText (LoginActivity. this, "incorrect user name 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 access address String resultloginMsgString = AgentApi. dopost (parmas, loginMsgurl); String resultfpasswordString = AgentApi. dopost (parmas, foundPassWordurl); // send the handler message msg. what = 1; msg. obj = resultloginMsgString;} catch (Exception e) {e. printStackTrace (); // use-1 to indicate that the program is abnormal msg. what =-2; msg. obj = e;} mHandler. sendMessage (msg );}}. start ();} return false;}/*** determine whether the user name and password are valid ** @ return */public boolean isUserNameAndPwdValid () {// the user name and password cannot be blank if (account. getText (). toString (). trim (). 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 take a look at the running effect. If both the user name and password are correct, you will be prompted if the user name and password are incorrect, as shown below:

Small message: This blog, Xiao Bian, briefly introduced how to implement Android login. But to be honest, Xiao Bian is still in the fog, and he is not proficient in xml layout pages, but it doesn't matter. The small editor will be more frustrated and brave. This is the meaning of life. Or that sentence is both a challenge and an opportunity for the small Editor, because the knowledge is the same. Furthermore, in the life of the mini-editor program, it leaves the most precious memories. Although later mini-Editor may not be engaged in the android industry, there are many kinds of things in the Code world, some are sweet and some are warm, some of these stories have evolved into songs, while others have continued. In these stories, the only thing we have in common is that in a year, a month, or a period of time, we love you very much! Love you-during this internship, Android has brought various surprises to xiaobian.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.