Android Implementation Login Feature Demo sample _android

Source: Internet
Author: User
Tags gettext sqlite database

The example in this article describes how Android implements the login feature. Share to everyone for your reference, specific as follows:

Andrew, in the years before the small part of the internship in the period, the small series have not played, if you have played, it is the Android phone, cough, knock log in when there is a special long time familiar, this familiar feeling and knocking at the computer room fee system, it is called a difficult ah, but small series believe that in the small series of it growth Road, It is because of these difficulties accompanied by small knitting together to grow, just let small make up more brave and strong, brave face one after another bug, strong knock out a line of code, after a few days of research to login a line function has been realized, now small make a simple summary, but also asked the small partners more advice oh ' (*∩_∩ *)′!

Overall Android architecture and we have learned before the three-tier architecture is still very similar, because the knowledge is interlinked, hey, so start up a bit more familiar sense, small set knocking on the idea is such, first of all, in the model inside the establishment of entities, Then in the res in the layout to create an XML file, the required page layout, so far, small series on the pendulum control has not much feeling, but also need to practice, and finally in the SRC UI inside to create a class to write specific logic. At the time of knocking on the Internet to download a lot of demo, but their architecture and small series now knock project structure is not very similar, so stones, all the way, small weave fall black and blue, but the process is very beautiful ' (*∩_∩*), Next, we look at the specific code implementation.

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

/*** * Description: Log in when necessary information * Author: Ding * Time: July 17, 2015 09:40:18 * * Package jczb.shoping.model;
Import java.io.Serializable; Import Android.
r.string;
  Log in required field information 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;
  The public void setgrowths (int growths) {this.growths = growths;
  Public String getmemberintegral () {return memberintegral;
  } public void Setmemberintegral (String memberintegral) {this.memberintegral = memberintegral;

 }
}

The second Foundpassword to use, as shown in the code below:

/*** * Description: Retrieve password Related information * Author: 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 vlidationnum; Authentication 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'll draw the page and use XML to make the layout settings for our login page, as shown in the following code:

<?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" a ndroid:background= "@color/login_background_color" > <!--the layout of Tesco--> <textview android:layout_width= "Mat Ch_parent "android:layout_height=" wrap_content "android:layout_margintop=" 30DP "android:gravity=" center "a ndroid:text= "Learn Tesco" android:textcolor= "@color/blue_ipsetting" android:textsize= "30sp" android:textstyle= "bold"/ > <!--the entire linearlayout is the account number and please enter account of the rectangular frame--> <linearlayout android:layout_width= "Fill_parent" a ndroid:layout_height= "Wrap_content" android:layout_margin= "5DP" android:orientation= "Horizontal" a
          ndroid: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:tex"
        t= "@string/loginaccount" android:textcolor= "@android: Color/black" android:textsize= "18.0sp"/> <!--EditText is to enter a layout of the account--> <edittext android:id= "@+id/loginaccount_id" Andro Id:layout_width= "160DP" android:layout_height= "Wrap_content" android:layout_weight= "1" Andro Id:background= "@null" android:ems= "android:hint=" @string/inputaccount "android:padding=" 5.0dip "android:paddingright=" 40DP "android:textcolor=" #ff3b3b3b "android:textsize=" 16.0SP " > </EditText> </LinearLayout> <!--password and please enter the entire layout of the password--> <linearlayout android:l Ayout_width= "Fill_parent" android:layout_height= "wrap_content" android:layout_margin= "5DP" Android:orientat ion= "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" Andro id:text= "@string/password" android:textcolor= "@android: Color/black" android:textsize= "18.0sp"/> < !--Please enter the layout of the password--> <edittext android:id= "@+id/password_id" android:layout_width= "163DP" Andr oid: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= "16.0sp"/> </line arlayout> <button android:id= "@+id/login" android:layout_width= "Fill_parent" Android:layout_h
   eight= "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 password and understand registered layout--> <!--retrieve password and immediate note The layout of the book--> <linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" and Roid:layout_margin= "5DP" android:background= "@color/white" android:orientation= "Horizontal" android:padding = "10SP" > <!--retrieve password--> <textview android:layout_width= "70DP" android:layout_height= "5 0DP "android:layout_marginleft=" 40DP "android:layout_margintop=" 15DP "android:text=" Find password "Andr Oid: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:la yout_weight= "0.03" android:text= "Register Now" android:textsize= "15SP" android:textstyle= "bold"/> <

 /linearlayout> </LinearLayout>

Finally, take a look at the code for the specific logical part, as shown in the following code:

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 {//DECLARE variable 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 monitor by the following onclick implementation loginbutton.setonclicklistener (this); @Override/** * Implement login button's jump/public void OnClick (View v) {//////////////////////////////
        CH (v.getid ()) {case R.id.login:login ();
      Break
      Default:break;  }/** * Login method/Public boolean login () {if (Isusernameandpwdvalid ()) {Mhandler=new
            Handler () {public void Handlemessage (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, wrong ...", Toast.length_short). Show (); BReak;
              Case 1:string temp= (String) msg.obj;
      Converts the obtained JSON 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)) {//implementation interface Jump Intent Intent = new Intent (logi
              Nactivity.this,mainactivity.class);
              StartActivity (Intent);
              Closes the current interface finish ();
             }else{//implementation interface of the jump Intent Intent = new Intent (loginactivity.this,mainactivity.class);
             StartActivity (Intent);
Closes the current interface finish (); Toast.maketext (loginactivity.tHis, "Username or password error", 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; /** * To determine whether the username and password are valid * * @return/public boolean isusernameandpwdvalid () {//username and password must not be is an empty 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_emp
        ty), Toast.length_short). Show ();
      return false;
    return true;

 }
}

Finally, let's look at the effect of the operation, if the username and password are correctly skipped to the home page, if the username and password errors are prompted, as shown in the screenshot below:

More interested readers of Android-related content can view this site: "Android Layout Layout Skills Summary", "Android View Overview", "The activity of Android programming skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android File Operation skills Summary", "Android programming development of SD card operation method Summary", " Android Development Primer and Advanced tutorials, Android Resource operations tips and Android Control usage summary

I hope this article will help you with the Android program.

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.