RegisterFragment.java
10.3 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.hjx.personalcenter.fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.hjx.personalcenter.R;
import com.hjx.personalcenter.activity.RegisterInfoActivity;
import com.hjx.personalcenter.http.HttpCode;
import com.hjx.personalcenter.http.HttpManager;
import com.hjx.personalcenter.util.AlertUtils;
import com.hjx.personalcenter.util.PasswordCheckUtils;
import com.hjx.personalcenter.util.PhoneNumCheckUtils;
import org.json.JSONException;
import org.json.JSONObject;
/**
* 注册
* Created by Administrator on 2016/11/7.
*/
public class RegisterFragment extends Fragment implements View.OnClickListener {
private View mView;
private Button btn_register,get_authcode;
private EditText phonenumber,password,anth_6num;//;//密码
private ImageView openeyes;
private String sourceStr ="android";
private String typeStr = "register";
private int i = 0;
private Thread thread;
private int type;
public static Boolean showPassword = false;
public static String TABLAYOUT_FRAGMENT = "tab_fragment";
public static RegisterFragment newInstance(int type) {
RegisterFragment fragment = new RegisterFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(TABLAYOUT_FRAGMENT, type);
fragment.setArguments(bundle);
return fragment;
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
try {
JSONObject jsonObject;
String status;
switch (msg.what) {
case HttpCode.REGISTERED_SUCESS:
jsonObject = (JSONObject) msg.obj;
status = jsonObject.getString("status");
//AlertUtils.showToast(RegisterActivity.this, jsonObject.optString("message"));
String access_token = jsonObject.getString("access_token");
String userId = jsonObject.getString("userId");
if (status.equals("100")) {
String usernameStr = phonenumber.getText().toString().trim();
String passwordStr = password.getText().toString().trim();
HttpManager.getInstance().saveLoginInfo(getActivity(),usernameStr,passwordStr,access_token,"true",userId);
AlertUtils.showToast(getActivity(), "注册成功!");
Intent intent = new Intent();
intent.setClass(getActivity(),RegisterInfoActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.rightin, R.anim.rightout);
getActivity().finish();
}
break;
case HttpCode.REGISTERED_FAIL:
AlertUtils.showToast(getActivity(), "注册失败");
break;
case HttpCode.AUTHCODE_SUCESS:
jsonObject = (JSONObject) msg.obj;
status = jsonObject.optString("status");
if (status.equals("100")) {
AlertUtils.showToast(getActivity(), "验证码发送成功");
}
break;
case HttpCode.AUTHCODE_FAIL:
jsonObject = (JSONObject) msg.obj;
AlertUtils.showToast(getActivity(), "验证码发送失败");
break;
case HttpCode.IS_REFISTER:
jsonObject = (JSONObject) msg.obj;
status = jsonObject.getString("status");
if (!status.equals("true")) {
getauthcode();
}else {
AlertUtils.showToast(getActivity(), "该手机号已经注册过了");
}
break;
case 1:
get_authcode.setEnabled(false);
get_authcode.setClickable(false);
get_authcode.setText(Integer.toString(i--)+" s");
if (i<=0){
get_authcode.setEnabled(true);
get_authcode.setClickable(true);
get_authcode.setText("获取验证码");
}
break;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.fragment_loginandregister_register, container, false);
initView(mView);
initData();
setLister();
}
return mView;
}
private void initView(View mView) {
get_authcode = (Button) mView.findViewById(R.id.btn_authcode);
anth_6num = (EditText) mView.findViewById(R.id.et_authcode);
phonenumber = (EditText) mView.findViewById(R.id.et_phonenumber);
password = (EditText) mView.findViewById(R.id.et_password);
openeyes= (ImageView) mView.findViewById(R.id.iv_pwd_change);
btn_register = (Button) mView.findViewById(R.id.btn_register);
}
private void initData() {
}
private void setLister() {
get_authcode.setOnClickListener(this);
openeyes.setOnClickListener(this);
btn_register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_register:
//测试
Intent intent = new Intent();
intent.setClass(getActivity(),RegisterInfoActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.rightin, R.anim.rightout);
getActivity().finish();
String usernameStr = phonenumber.getText().toString().trim();
String authcodeStr = anth_6num.getText().toString().trim();
String passwordStr = password.getText().toString().trim();
if (TextUtils.isEmpty(usernameStr) || TextUtils.isEmpty(authcodeStr)||TextUtils.isEmpty(passwordStr)) {
AlertUtils.showToast(getActivity(), "手机号,密码和验证码不能为空");
return;
} else if(passwordStr.length()<8){
AlertUtils.showToast(getActivity(), "密码不能少于8位");
}else if (!PhoneNumCheckUtils.isPhone(usernameStr)){
AlertUtils.showToast(getActivity(), "手机号码输入错误");
}else if(!passwordStr.matches(PasswordCheckUtils.PASSWORD_CHAR_AND_NUMBER)){
AlertUtils.showToast(getActivity(), "密码由英文和数字组成");
}else {
register();
}
break;
case R.id.iv_pwd_change:
if(showPassword){//显示密码
showPassword = !showPassword;
openeyes.setImageResource(R.mipmap.pwd_open);
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
password.setSelection(password.getText().toString().length());
}else{//隐藏密码
showPassword = !showPassword;
openeyes.setImageResource(R.mipmap.pwd_hide);
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
password.setSelection(password.getText().toString().length());
}
break;
case R.id.btn_authcode:
usernameStr = phonenumber.getText().toString().trim();
if (!PhoneNumCheckUtils.isPhone(usernameStr)){
AlertUtils.showToast(getActivity(), "请输入正确的手机号");
}else {
anth_6num.requestFocus();
ifregister();
}
break;
}
}
//获取验证码
private void getauthcode() {
String usernameStr = phonenumber.getText().toString().trim();
HttpManager.getInstance().authCode(typeStr, usernameStr, handler, getActivity());
i = 60;
if(thread == null){
thread = new Thread( new ThreadShow());
thread.start();
}
}
private void ifregister() {
String usernameStr = phonenumber.getText().toString().trim();
HttpManager.getInstance().isregistered(usernameStr, handler, getActivity());
}
private void register() {
String usernameStr = phonenumber.getText().toString().trim();
String authcodeStr = anth_6num.getText().toString().trim();
String passwordStr = password.getText().toString().trim();
HttpManager.getInstance().register(getActivity(), usernameStr, passwordStr, authcodeStr, sourceStr,handler);
}
// 线程类 定时器
class ThreadShow implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
System.out.println("send...");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("thread error...");
}
}
}
}
}