最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實現(xiàn)登錄功能demo示例

 更新時間:2016年07月09日 09:01:56   作者:丁國華  
這篇文章主要介紹了Android實現(xiàn)登錄功能demo示例,涉及登錄信息操作、界面布局、登錄邏輯判斷等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了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文件,對所需要的頁面進(jìn)行相關(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; //確認(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對我們的登錄頁面進(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" />
  <!-- 整個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ā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Flutter實現(xiàn)一個支持漸變背景的Button示例詳解

    Flutter實現(xiàn)一個支持漸變背景的Button示例詳解

    這篇文章主要為大家介紹了Flutter實現(xiàn)一個支持漸變背景的Button示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 詳解房卡麻將分析系列

    詳解房卡麻將分析系列 "牌局回放" 之 播放處理

    這篇文章主要介紹了詳解房卡麻將分析系列 "牌局回放" 之 播放處理的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Android?創(chuàng)建AIDL文件使用教程

    Android?創(chuàng)建AIDL文件使用教程

    這篇文章主要介紹了Android創(chuàng)建AIDL文件使用教程,AIDL是一種IDL語言,用于生成可以在Android設(shè)備上兩個進(jìn)程之間進(jìn)行進(jìn)程間通信的代碼
    2022-07-07
  • Android仿微信頁面底部導(dǎo)航效果代碼實現(xiàn)

    Android仿微信頁面底部導(dǎo)航效果代碼實現(xiàn)

    本文給大家分享一段代碼有關(guān)android仿微信頁面底部導(dǎo)航效果代碼實現(xiàn)的思路,非常不錯,感興趣的朋友一起看看吧
    2016-09-09
  • android app icon 圖標(biāo)大小尺寸

    android app icon 圖標(biāo)大小尺寸

    應(yīng)用程序圖標(biāo) (Icon)應(yīng)當(dāng)是一個 Alpha 通道透明的32位 PNG 圖片。由于安卓設(shè)備眾多,一個應(yīng)用程序圖標(biāo)需要設(shè)計幾種不同大小。
    2016-05-05
  • Android播放視頻的三種方式

    Android播放視頻的三種方式

    這篇文章主要為大家詳細(xì)介紹了Android播放視頻的三種方式,使用其自帶的播放器、VideoView、MediaPlayer類和SurfaceView來實現(xiàn),感興趣的小伙伴們可以參考一下
    2016-07-07
  • 最新評論

    阿巴嘎旗| 南城县| 福贡县| 澎湖县| 秭归县| 三都| 密云县| 仙游县| 合山市| 武威市| 历史| 论坛| 平潭县| 德化县| 介休市| 鄂尔多斯市| 呈贡县| 柳江县| 祁门县| 建宁县| 英吉沙县| 滁州市| 临漳县| 安顺市| 濮阳市| 故城县| 万盛区| 宿州市| 亚东县| 乳山市| 斗六市| 福安市| 酒泉市| 松溪县| 印江| 崇州市| 左权县| 阆中市| 岳普湖县| 宜丰县| 弋阳县|