Android實(shí)現(xiàn)登錄功能demo示例
本文實(shí)例講述了Android實(shí)現(xiàn)登錄功能的方法。分享給大家供大家參考,具體如下:
安卓,在小編實(shí)習(xí)之前的那段歲月里面,小編都沒有玩兒過,如果說玩兒過,那就是安卓手機(jī)了,咳咳,敲登錄的時(shí)候有種特別久違的熟悉,這種熟悉的感覺就和當(dāng)時(shí)敲機(jī)房收費(fèi)系統(tǒng)一樣,那叫一個(gè)艱難啊,不過小編相信,在小編的IT成長之路上,正是因?yàn)橛辛诉@些艱難險(xiǎn)阻陪伴著小編一起成長,才讓小編更加勇敢堅(jiān)強(qiáng),勇敢的面對(duì)一個(gè)又一個(gè)bug,堅(jiān)強(qiáng)的敲完一行行代碼,經(jīng)過了幾天的研究登錄一條線的功能已經(jīng)實(shí)現(xiàn),現(xiàn)在小編就來簡單的總結(jié)一下,還請(qǐng)小伙伴們多多指教哦`(*∩_∩*)′!
總的來說Android的架構(gòu)和我們之前學(xué)習(xí)過的三層架構(gòu)還是很相似的,因?yàn)橹R(shí)都是相通的嘛,嘿嘿,這樣入手起來多了幾分熟悉感,小編敲登錄的思路是這樣的,首先,在model里面建立實(shí)體,接著在res中的layout里面建立一個(gè)XML文件,對(duì)所需要的頁面進(jìn)行相關(guān)布局,至此,小編對(duì)擺控件還沒有多大的感覺,還需要多多練習(xí),最后在src中ui里面建立一個(gè)類寫具體的邏輯。在敲登錄的時(shí)候小編在網(wǎng)上下載了很多demo,但是她們的架構(gòu)和小編現(xiàn)在所敲項(xiàng)目的架構(gòu)不是很相似,所以就摸著石頭過河,一路走來,小編摔得鼻青臉腫,但是過程很美麗`(*∩_∩*)′,接下來,我們來看具體的代碼實(shí)現(xiàn)。
首先,我們需要在model里面建立實(shí)體,由于小編用到了兩張表,所以需要建立兩個(gè)model,第一個(gè)用到的是LoginMsg,代碼如下:
/*** * 說明:登錄時(shí)所需要的相關(guān)信息 * 作者:丁國華 * 時(shí)間:2015年7月17日 09:40:18 */ package jczb.shoping.model; import java.io.Serializable; import android.R.string; //登錄所需要的字段信息 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; } }
第二個(gè)用到的FoundPassWord,具體代碼如下所示:
/*** * 說明:找回密碼的相關(guān)信息 * 作者:丁國華 * 時(shí)間:2015年7月17日 09:41:18 */ package jczb.shoping.model; import java.io.Serializable; import android.R.string; public class foundPassWord implements Serializable { private String phoneNumber; //手機(jī)號(hào) private String vlidationNum; //驗(yàn)證碼 private String passWord; //密碼 private String repetyPassWord; //確認(rèn)密碼 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; } }
接著,我們來畫頁面,用xml對(duì)我們的登錄頁面進(jìn)行相關(guān)布局設(shè)置,具體代碼如下所示:
<?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"> <!-- 學(xué)樂購的布局 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:gravity="center" android:text="學(xué)樂購" android:textColor="@color/blue_ipsetting" android:textSize="30sp" android:textStyle="bold" /> <!-- 整個(gè)LinearLayout是賬號(hào)和請(qǐng)輸入賬號(hào)的矩形框架 --> <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" > <!-- 帳號(hào)的布局 --> <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="18.0sp" /> <!-- EditText表示的是請(qǐng)輸入帳號(hào)的一個(gè)布局 --> <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="16.0sp" > </EditText> </LinearLayout> <!-- 密碼和請(qǐng)輸入密碼的整個(gè)布局 --> <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"> <!-- 密碼的布局 --> <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"/> <!-- 請(qǐng)輸入密碼的布局 --> <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="16.0sp"/> </LinearLayout> <Button android: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" /> <!-- 找回密碼和理解注冊(cè)的布局 --> <!-- 找回密碼和立即注冊(cè)的布局 --> <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" > <!-- 找回密碼 --> <TextView android:layout_width="70dp" android:layout_height="50dp" android:layout_marginLeft="40dp" android:layout_marginTop="15dp" android:text="找回密碼" android:textSize="15sp" android:textStyle="bold" /> <!-- 立即注冊(cè) --> <TextView android:layout_width="70dp" android:layout_height="56dp" android:layout_marginLeft="55dp" android:layout_marginTop="15dp" android:layout_weight="0.03" android:text="立即注冊(cè)" android:textSize="15sp" android:textStyle="bold" /> </LinearLayout> </LinearLayout>
最后,來看一下具體的邏輯部分的代碼,具體代碼如下所示:
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 { //聲明變量 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); // 對(duì)登錄按鈕設(shè)置監(jiān)聽,方法由下面的Onclick實(shí)現(xiàn) loginButton.setOnClickListener(this); } @Override /** * 實(shí)現(xiàn)登錄按鈕的跳轉(zhuǎn) */ public void onClick(View v) { // 根據(jù)id判斷單擊的是哪個(gè)控件,固定寫法 switch (v.getId()) { case R.id.login: login(); break; default: break; } } /** * 登錄方法 */ public boolean login() { if (isUserNameAndPwdValid()) { mHandler=new Handler(){ public void handleMessage(Message msg){ switch(msg.what){ case -1: Toast.makeText(LoginActivity.this,"服務(wù)器連接失敗!", Toast.LENGTH_SHORT).show(); break; case -2: Toast.makeText(LoginActivity.this,"哎呀,出錯(cuò)啦...", Toast.LENGTH_SHORT).show(); break; case 1: String temp=(String)msg.obj; //將拿到的json轉(zhuǎn)換成數(shù)組 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)) { //實(shí)現(xiàn)界面的跳轉(zhuǎn) Intent intent = new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); //關(guān)閉當(dāng)前界面 finish(); }else{ //實(shí)現(xiàn)界面的跳轉(zhuǎn) Intent intent = new Intent(LoginActivity.this,MainActivity.class); startActivity(intent); //關(guān)閉當(dāng)前界面 finish(); // Toast.makeText(LoginActivity.this, "用戶名或密碼錯(cuò)誤", 0).show(); } } } }; //主線程 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"; //要發(fā)送的數(shù)據(jù)和訪問的地址 String resultloginMsgString=AgentApi.dopost(parmas,loginMsgurl); String resultfpasswordString=AgentApi.dopost(parmas, foundPassWordurl); //發(fā)送handler信息 msg.what=1; msg.obj=resultloginMsgString; }catch(Exception e){ e.printStackTrace(); //使用-1代表程序異常 msg.what=-2; msg.obj=e; } mHandler.sendMessage(msg); } }.start(); } return false; } /** * 判斷用戶名和密碼是否有效 * * @return */ public boolean isUserNameAndPwdValid() { // 用戶名和密碼不得為空 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; } }
最后,我們來看一下運(yùn)行效果,如果用戶名和密碼都正確跳到主頁,如果用戶名和密碼錯(cuò)誤給出提示,如下截圖所示:
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android手機(jī)注冊(cè)登錄時(shí)獲取驗(yàn)證碼之后倒計(jì)時(shí)功能(知識(shí)點(diǎn)總結(jié))
- Android實(shí)現(xiàn)登錄注冊(cè)功能封裝
- Android客戶端實(shí)現(xiàn)注冊(cè)、登錄詳解(2)
- Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果
- Android客戶端實(shí)現(xiàn)注冊(cè)、登錄詳解(1)
- Android注冊(cè)登錄實(shí)時(shí)自動(dòng)獲取短信驗(yàn)證碼
- Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能
- Android開發(fā)之注冊(cè)登錄方法示例
- Android開發(fā)之登錄驗(yàn)證實(shí)例教程
- Android登錄注冊(cè)功能 數(shù)據(jù)庫SQLite驗(yàn)證
相關(guān)文章
Android應(yīng)用中拍照后獲取照片路徑并上傳的實(shí)例分享
這篇文章主要介紹了Android應(yīng)用中拍照后獲取照片路徑并上傳的實(shí)例分享,文中使用MultipartEntityBuilder制作了一個(gè)簡單的上傳工具,需要的朋友可以參考下2016-03-03在Android里完美實(shí)現(xiàn)基站和WIFI定位
眾所周知的,在OPhone和大部分國產(chǎn)的Android定制機(jī)里不支持最簡單實(shí)用的基站和WIFI定位,只能使用速度慢而耗電的GPS定位,但OPhone和華為/中興生產(chǎn)的一些Android定制機(jī)卻占據(jù)了一定的市場,因此導(dǎo)致了很多使用了定位技術(shù)的Andorid應(yīng)用挺尷尬的。2014-07-07android實(shí)現(xiàn)banner輪播圖無限輪播效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)banner輪播圖無限輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android開發(fā)實(shí)現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法,結(jié)合實(shí)例形式分析了Android ImageView界面布局及元素屬性動(dòng)態(tài)操作兩種功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02Android通過HttpURLConnection和HttpClient接口實(shí)現(xiàn)網(wǎng)絡(luò)編程
這篇文章主要介紹了Android通過HttpURLConnection和HttpClient接口實(shí)現(xiàn)網(wǎng)絡(luò)編程的相關(guān)資料,需要的朋友可以參考下2015-02-02Android開發(fā)簡單實(shí)現(xiàn)搖動(dòng)動(dòng)畫的方法
這篇文章主要介紹了Android開發(fā)簡單實(shí)現(xiàn)搖動(dòng)動(dòng)畫的方法,結(jié)合實(shí)例形式分析了Android搖動(dòng)動(dòng)畫的布局與功能簡單實(shí)現(xiàn)方法,需要的朋友可以參考下2017-10-10基于Android實(shí)現(xiàn)仿QQ5.0側(cè)滑
本課程將帶領(lǐng)大家通過自定義控件實(shí)現(xiàn)QQ5.0側(cè)滑菜單,課程將循序漸進(jìn),首先實(shí)現(xiàn)最普通的側(cè)滑菜單,然后引入屬性動(dòng)畫與拖動(dòng)菜單效果相結(jié)合,最終實(shí)現(xiàn)QQ5.0側(cè)滑菜單效果。通過本課程大家會(huì)對(duì)側(cè)滑菜單有更深層次的了解,通過自定義控件和屬性動(dòng)畫打造千變?nèi)f化的側(cè)滑菜單效果2015-12-12Android Jetpack組件庫LiveData源碼深入探究
LiveData是Jetpack組件的一部分,更多的時(shí)候是搭配ViewModel來使用,相對(duì)于Observable,LiveData的最大優(yōu)勢是其具有生命感知的,換句話說,LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動(dòng)生命周期狀態(tài)的時(shí)候才會(huì)更新數(shù)據(jù)2022-09-09