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,具体代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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对我们的登录页面进行相关布局设置,具体代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
<? 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" >
<!-- 学乐购的布局 -->
< TextView
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_marginTop = "30dp"
android:gravity = "center"
android:text = "学乐购"
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 >
|
最后,来看一下具体的逻辑部分的代码,具体代码如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
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);
// 对登录按钮设置监听,方法由下面的Onclick实现
loginButton.setOnClickListener( this );
}
@Override
/**
* 实现登录按钮的跳转
*/
public void onClick(View v) {
// 根据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 , "服务器连接失败!" ,
Toast.LENGTH_SHORT).show();
break ;
case - 2 :
Toast.makeText(LoginActivity. this , "哎呀,出错啦..." ,
Toast.LENGTH_SHORT).show();
break ;
case 1 :
String temp=(String)msg.obj;
//将拿到的json转换成数组
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)) {
//实现界面的跳转
Intent intent = new Intent(LoginActivity. this ,MainActivity. class );
startActivity(intent);
//关闭当前界面
finish();
} else {
//实现界面的跳转
Intent intent = new Intent(LoginActivity. this ,MainActivity. class );
startActivity(intent);
//关闭当前界面
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" ;
//要发送的数据和访问的地址
String resultloginMsgString=AgentApi.dopost(parmas,loginMsgurl);
String resultfpasswordString=AgentApi.dopost(parmas, foundPassWordurl);
//发送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 ;
}
}
|
最后,我们来看一下运行效果,如果用户名和密码都正确跳到主页,如果用户名和密码错误给出提示,如下截图所示: