Android實現(xiàn)登錄功能demo示例
本文實例講述了Android實現(xiàn)登錄功能的方法。分享給大家供大家參考,具體如下:
安卓,在小編實習(xí)之前的那段歲月里面,小編都沒有玩兒過,如果說玩兒過,那就是安卓手機了,咳咳,敲登錄的時候有種特別久違的熟悉,這種熟悉的感覺就和當(dāng)時敲機房收費系統(tǒng)一樣,那叫一個艱難啊,不過小編相信,在小編的IT成長之路上,正是因為有了這些艱難險阻陪伴著小編一起成長,才讓小編更加勇敢堅強,勇敢的面對一個又一個bug,堅強的敲完一行行代碼,經(jīng)過了幾天的研究登錄一條線的功能已經(jīng)實現(xiàn),現(xiàn)在小編就來簡單的總結(jié)一下,還請小伙伴們多多指教哦`(*∩_∩*)′!
總的來說Android的架構(gòu)和我們之前學(xué)習(xí)過的三層架構(gòu)還是很相似的,因為知識都是相通的嘛,嘿嘿,這樣入手起來多了幾分熟悉感,小編敲登錄的思路是這樣的,首先,在model里面建立實體,接著在res中的layout里面建立一個XML文件,對所需要的頁面進行相關(guān)布局,至此,小編對擺控件還沒有多大的感覺,還需要多多練習(xí),最后在src中ui里面建立一個類寫具體的邏輯。在敲登錄的時候小編在網(wǎng)上下載了很多demo,但是她們的架構(gòu)和小編現(xiàn)在所敲項目的架構(gòu)不是很相似,所以就摸著石頭過河,一路走來,小編摔得鼻青臉腫,但是過程很美麗`(*∩_∩*)′,接下來,我們來看具體的代碼實現(xiàn)。
首先,我們需要在model里面建立實體,由于小編用到了兩張表,所以需要建立兩個model,第一個用到的是LoginMsg,代碼如下:
/***
* 說明:登錄時所需要的相關(guān)信息
* 作者:丁國華
* 時間: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;
}
}
第二個用到的FoundPassWord,具體代碼如下所示:
/***
* 說明:找回密碼的相關(guān)信息
* 作者:丁國華
* 時間: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; //手機號
private String vlidationNum; //驗證碼
private String passWord; //密碼
private String repetyPassWord; //確認密碼
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對我們的登錄頁面進行相關(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" />
<!-- 整個LinearLayout是賬號和請輸入賬號的矩形框架 -->
<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_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表示的是請輸入帳號的一個布局 -->
<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>
<!-- 密碼和請輸入密碼的整個布局 -->
<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"/>
<!-- 請輸入密碼的布局 -->
<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" />
<!-- 找回密碼和理解注冊的布局 -->
<!-- 找回密碼和立即注冊的布局 -->
<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" />
<!-- 立即注冊 -->
<TextView
android:layout_width="70dp"
android:layout_height="56dp"
android:layout_marginLeft="55dp"
android:layout_marginTop="15dp"
android:layout_weight="0.03"
android:text="立即注冊"
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);
// 對登錄按鈕設(shè)置監(jiān)聽,方法由下面的Onclick實現(xiàn)
loginButton.setOnClickListener(this);
}
@Override
/**
* 實現(xiàn)登錄按鈕的跳轉(zhuǎn)
*/
public void onClick(View v) {
// 根據(jù)id判斷單擊的是哪個控件,固定寫法
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,"哎呀,出錯啦...",
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)) {
//實現(xiàn)界面的跳轉(zhuǎn)
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
//關(guān)閉當(dāng)前界面
finish();
}else{
//實現(xiàn)界面的跳轉(zhuǎn)
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
startActivity(intent);
//關(guān)閉當(dāng)前界面
finish();
// Toast.makeText(LoginActivity.this, "用戶名或密碼錯誤", 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;
}
}
最后,我們來看一下運行效果,如果用戶名和密碼都正確跳到主頁,如果用戶名和密碼錯誤給出提示,如下截圖所示:

更多關(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ā)入門與進階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android應(yīng)用中拍照后獲取照片路徑并上傳的實例分享
這篇文章主要介紹了Android應(yīng)用中拍照后獲取照片路徑并上傳的實例分享,文中使用MultipartEntityBuilder制作了一個簡單的上傳工具,需要的朋友可以參考下2016-03-03
android實現(xiàn)banner輪播圖無限輪播效果
這篇文章主要為大家詳細介紹了android實現(xiàn)banner輪播圖無限輪播效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android開發(fā)實現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)ImageView寬度頂邊顯示,高度保持比例的方法,結(jié)合實例形式分析了Android ImageView界面布局及元素屬性動態(tài)操作兩種功能實現(xiàn)技巧,需要的朋友可以參考下2018-02-02
Android通過HttpURLConnection和HttpClient接口實現(xiàn)網(wǎng)絡(luò)編程
這篇文章主要介紹了Android通過HttpURLConnection和HttpClient接口實現(xiàn)網(wǎng)絡(luò)編程的相關(guān)資料,需要的朋友可以參考下2015-02-02
Android開發(fā)簡單實現(xiàn)搖動動畫的方法
這篇文章主要介紹了Android開發(fā)簡單實現(xiàn)搖動動畫的方法,結(jié)合實例形式分析了Android搖動動畫的布局與功能簡單實現(xiàn)方法,需要的朋友可以參考下2017-10-10
Android Jetpack組件庫LiveData源碼深入探究
LiveData是Jetpack組件的一部分,更多的時候是搭配ViewModel來使用,相對于Observable,LiveData的最大優(yōu)勢是其具有生命感知的,換句話說,LiveData可以保證只有在組件( Activity、Fragment、Service)處于活動生命周期狀態(tài)的時候才會更新數(shù)據(jù)2022-09-09

