"Android" Android Login interface design and network connection and authentication login

Source: Internet
Author: User
Tags getcolor

Androidmanifest.xml

<android:name= "Android.permission.INTERNET"></  Uses-permission><android:name= "Android.permission.ACCESS_ Network_state "/>

Layout file: Activity_main.xml

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"     >    <EditTextAndroid:id= "@+id/name"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignparenttop= "true"Android:layout_centerhorizontal= "true"Android:layout_margintop= "38DP"Android:ems= "Ten"Android:hint= "Account">        <Requestfocus/>    </EditText>    <EditTextAndroid:id= "@+id/password"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/name"Android:layout_centerhorizontal= "true"Android:layout_margintop= "17DP"Android:ems= "Ten"Android:hint= "Password"Android:inputtype= "Textpassword" />    <TextViewAndroid:id= "@+id/info"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@+id/password"Android:layout_centerhorizontal= "true"Android:layout_margintop= "17DP"android:gravity= "Center"Android:text= "Welcome to use this product"        />    <ButtonAndroid:id= "@+id/exit"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/name"Android:layout_below= "@+id/info"Android:layout_marginleft= "16DP"Android:layout_margintop= "16DP"Android:text= "Leave" />    <ButtonAndroid:id= "@+id/login"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignbaseline= "@+id/exit"Android:layout_alignbottom= "@+id/exit"Android:layout_alignright= "@+id/name"Android:layout_marginright= "16DP"Android:text= "Login" /></Relativelayout>

Mainactivity.java

Click the login button:

if(nameet!=NULL&&!nameet.gettext (). toString (). Equals ("") &&passwordet!=NULL&&!passwordet.gettext (). toString (). Equals ("") {LOG.E ("Login", "Login"); if(isconnect (mainactivity.this)!=false) {ProgressDialog= Progressdialog.show (mainactivity. This, "Please Wait", "Landing ...",true,false); } Thread T=NewThread (Newmyrunnable (Nameet. GetText (). toString (), Passwordet.gettext ().                    ToString ()));              T.start (); }Else{LOG.E ("Errors", "Error"); if(nameet==NULL|| Nameet.gettext (). toString (). Equals ("") {nameet.seterror ("Please enter a name"); }                  if(passwordet==NULL|| Passwordet.gettext (). toString (). Equals ("") {passwordet.seterror ("Please enter your password"); }              }

To open a thread:

 public  class  myrunnable implements   Runnable {String name, passcode;  public   Myrunnable (string name, string Pass) { //  TODO auto-generated Constr            Uctor stub  this . Name = name;         this . Passcode = pass; } @Override  public  void   run () {//*** Here for network connection, get data  * * * * 
// Use Get or post***

Data is added to the bundle

Message msg = new Message ();
Bundle bundle = new bundle ();//Store data

Bundle.putstring ("ReturnCode", data);

Msg.setdata (bundle);

Handler.sendmessage (msg); Send a message to handler to update the UI

                       }}        

Determine if there is no network:

 Public Static BooleanIsconnect (mainactivity activity) {//Get all connection management objects for your phone (including management of connections such as Wi-fi,net)        Try{Connectivitymanager Connectivity=( Connectivitymanager) (activity). Getsystemservice (Context.connectivity_service); if(Connectivity! =NULL) {                //get the objects for network connection managementNetworkinfo info =Connectivity.getactivenetworkinfo (); if(Info! =NULL&&info.isconnected ()) {                    //determine if the current network is connected                    if(info.getstate () = =NetworkInfo.State.CONNECTED) {return true; }                }            }        } Catch(Exception e) {//Todo:handle ExceptionLOG.V ("Error", e.tostring ()); }        return false; } 

The handler mechanism refreshes the UI:

Handler Handler =NewHandler () {@Override Public voidhandlemessage (Message msg) {//TODO auto-generated Method StubBundle B =Msg.getdata (); String ReturnCode= B.getstring ("ReturnCode"); if(Returncode.equals ("0")) {                /**return code, 0-Login successful, 1-account password error, server maintenance, 3-Unknown error*/                                //Close ProgressDialogProgressdialog.dismiss (); Infotv.settext ("Successful Landing"); } Else if(Returncode.equals ("1") ) {Progressdialog.dismiss (); Infotv.settext ("Incorrect account password"); Infotv.settextcolor (Getresources (). GetColor (Android.            R.color.holo_red_dark)); } Else if(Returncode.equals ("2") ) {Progressdialog.dismiss (); Infotv.settext ("Server Maintenance"); Infotv.settextcolor (Getresources (). GetColor (Android.            R.color.holo_red_dark)); } Else if(Returncode.equals ("3") ) {Progressdialog.dismiss (); Infotv.settext ("Unknown Error"); Infotv.settextcolor (Getresources (). GetColor (Android.            R.color.holo_red_dark)); } Else if(Statuscodestr.equals ("StatusCode") ) {Progressdialog.dismiss (); }        }    };

"Android" Android Login interface design and network connection and authentication login

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.