1 <?xml version="1.0" encoding="utf-8"?>
2
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical"
6 android:background="@drawable/bg_01">"7
8
10 android:layout_height="wrap_content"
11 android:text="注册"
12 android:textSize="22dip"
13 android:textColor="#FFFFFF"
14 android:paddingLeft="140dip"
15 android:paddingRight="50dip"
16 android:paddingTop="10dip"
17 android:background="@drawable/topbg"
18 />
19 "20
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:layout_marginTop="20dip"
25 android:background="@drawable/search"
26 android:layout_marginLeft="20dip"
27 android:layout_marginRight="20dip"
28 android:height="40dip"
29 android:hint="用户名"
30 />
31
32
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:layout_marginTop="20dip"
37 android:background="@drawable/search"
38 android:layout_marginLeft="20dip"
39 android:layout_marginRight="20dip"
40 android:height="40dip"
41 android:hint="密码"
42 />
43
44
46 android:layout_width="wrap_content"
47 android:layout_height="wrap_content"
48 android:layout_marginTop="20dip"
49 android:background="@drawable/search"
50 android:layout_marginLeft="20dip"
51 android:layout_marginRight="20dip"
52 android:height="40dip"
53 android:hint="确认密码"
54 />
55
57 android:layout_width="wrap_content"
58 android:layout_height="wrap_content"
59 android:background="@drawable/topbg"
60 android:height="40dip"
61 android:width="70dip"
62 android:layout_marginTop="60dip"
63 android:text="确定"
64 android:textColor="#FFFFFF"
65 android:textSize="22dip"
66
67 />
68
69
70
71 处理注册页面的Activity:72
73 packagecom.example.foreveross.office;74
75 importjava.io.IOException;76 importjava.io.UnsupportedEncodingException;77 importjava.util.ArrayList;78 importjava.util.List;79
80 importorg.apache.http.HttpEntity;81 importorg.apache.http.HttpResponse;82 importorg.apache.http.NameValuePair;83 importorg.apache.http.ParseException;84 importorg.apache.http.client.ClientProtocolException;85 importorg.apache.http.client.HttpClient;86 importorg.apache.http.client.entity.UrlEncodedFormEntity;87 importorg.apache.http.client.methods.HttpPost;88 importorg.apache.http.impl.client.DefaultHttpClient;89 importorg.apache.http.message.BasicNameValuePair;90 importorg.apache.http.util.EntityUtils;91
92 importcom.example.wenandroid.R;93 importandroid.app.Activity;94 importandroid.os.Bundle;95 importandroid.os.StrictMode;96 importandroid.view.View;97 importandroid.view.View.OnClickListener;98 importandroid.view.View.OnFocusChangeListener;99 importandroid.widget.Button;100 importandroid.widget.EditText;101 importandroid.widget.Toast;102
103 public class UserRegister extendsActivity {104
105 privateEditText register_username;106 privateEditText register_passwd;107 privateEditText reregister_passwd;108 privateButton register_submit;109 @Override110 protected voidonCreate(Bundle savedInstanceState) {111 //TODO Auto-generated method stub
112 super.onCreate(savedInstanceState);113 StrictMode.ThreadPolicy policy = newStrictMode.ThreadPolicy.Builder().permitAll().build();114 StrictMode.setThreadPolicy(policy);115 setContentView(R.layout.user_register);116 register_username=(EditText)findViewById(R.id.register_username);117 register_passwd=(EditText)findViewById(R.id.register_passwd);118 reregister_passwd=(EditText)findViewById(R.id.reregister_passwd);119 register_submit=(Button)findViewById(R.id.register_submit);120 register_username.setOnFocusChangeListener(newOnFocusChangeListener()121 {122
123 @Override124 public void onFocusChange(View v, booleanhasFocus) {125 //TODO Auto-generated method stub
126 if(!hasFocus){127 if(register_username.getText().toString().trim().length()<4){128 Toast.makeText(UserRegister.this, "用户名不能小于4个字符", Toast.LENGTH_SHORT).show();129 }130 }131 }132
133 });134 register_passwd.setOnFocusChangeListener(newOnFocusChangeListener()135 {136
137 @Override138 public void onFocusChange(View v, booleanhasFocus) {139 //TODO Auto-generated method stub
140 if(!hasFocus){141 if(register_passwd.getText().toString().trim().length()<6){142 Toast.makeText(UserRegister.this, "密码不能小于8个字符", Toast.LENGTH_SHORT).show();143 }144 }145 }146
147 });148 reregister_passwd.setOnFocusChangeListener(newOnFocusChangeListener()149 {150
151 @Override152 public void onFocusChange(View v, booleanhasFocus) {153 //TODO Auto-generated method stub
154 if(!hasFocus){155 if(!reregister_passwd.getText().toString().trim().equals(register_passwd.getText().toString().trim())){156 Toast.makeText(UserRegister.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();157 }158 }159 }160
161 });162 register_submit.setOnClickListener(newOnClickListener(){163
164 @Override165 public voidonClick(View v) {166
167 if(!checkEdit()){168 return;169 }170 //TODO Auto-generated method stub
171 String httpUrl="http://192.168.1.100:8080/web-test/register.jsp";172 HttpPost httpRequest=newHttpPost(httpUrl);173 List params=new ArrayList();174 params.add(new BasicNameValuePair("username",register_username.getText().toString().trim()));175 params.add(new BasicNameValuePair("password",register_passwd.getText().toString().trim()));176 HttpEntity httpentity = null;177 try{178 httpentity = new UrlEncodedFormEntity(params,"utf8");179 } catch(UnsupportedEncodingException e) {180 //TODO Auto-generated catch block
181 e.printStackTrace();182 }183 httpRequest.setEntity(httpentity);184 HttpClient httpclient=newDefaultHttpClient();185 HttpResponse httpResponse = null;186 try{187 httpResponse =httpclient.execute(httpRequest);188 } catch(ClientProtocolException e) {189 //TODO Auto-generated catch block
190 e.printStackTrace();191 } catch(IOException e) {192 //TODO Auto-generated catch block
193 e.printStackTrace();194 }195 if(httpResponse.getStatusLine().getStatusCode()==200)196 {197 String strResult = null;198 try{199 strResult =EntityUtils.toString(httpResponse.getEntity());200 } catch(ParseException e) {201 //TODO Auto-generated catch block
202 e.printStackTrace();203 } catch(IOException e) {204 //TODO Auto-generated catch block
205 e.printStackTrace();206 }207 Toast.makeText(UserRegister.this, strResult, Toast.LENGTH_SHORT).show();208 }209 else
210 {211 Toast.makeText(UserRegister.this, "请求错误", Toast.LENGTH_SHORT).show();212 }213
214 }215
216 });217 }218
219 private booleancheckEdit(){220 if(register_username.getText().toString().trim().equals("")){221 Toast.makeText(UserRegister.this, "用户名不能为空", Toast.LENGTH_SHORT).show();222 }else if(register_passwd.getText().toString().trim().equals("")){223 Toast.makeText(UserRegister.this, "密码不能为空", Toast.LENGTH_SHORT).show();224 }else if(!register_passwd.getText().toString().trim().equals(reregister_passwd.getText().toString().trim())){225 Toast.makeText(UserRegister.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();226 }else{227 return true;228 }229 return false;230 }231
232 }233
234 登录页面xml:235
236 user_login.xml:237
238 <?xml version="1.0" encoding="utf-8"?>
239
240 android:layout_width="fill_parent"
241 android:layout_height="fill_parent"
242 android:orientation="vertical"
243 android:background="@drawable/bg_01">
244
245
247 android:layout_height="wrap_content"
248 android:text="登录"
249 android:textSize="22dip"
250 android:textColor="#FFFFFF"
251 android:paddingLeft="140dip"
252 android:paddingRight="50dip"
253 android:paddingTop="10dip"
254 android:background="@drawable/topbg"
255 />
256
257
259 android:layout_height="wrap_content"
260 android:orientation="vertical" >
261
262
264 android:layout_width="fill_parent"
265 android:layout_height="40dip"
266 android:layout_marginLeft="20dip"
267 android:layout_marginRight="20dip"
268 android:layout_marginTop="30dip"
269 android:hint="用户名"
270 android:paddingTop="10dip"
271 android:textSize="18dip"
272 android:background="@drawable/search">
273
274
275
276
278 android:layout_width="fill_parent"
279 android:layout_height="40dip"
280 android:layout_marginLeft="20dip"
281 android:layout_marginRight="20dip"
282 android:layout_marginTop="10dip"
283 android:password="true"
284 android:paddingTop="10dip"
285 android:textSize="18dip"
286 android:hint="密码"
287 android:background="@drawable/search">
288
289
290
291
292
294 android:layout_height="wrap_content"
295 android:layout_gravity="center_horizontal"
296 android:layout_marginTop="15dip">
297
298
300 android:layout_width="wrap_content"
301 android:layout_height="wrap_content"
302 android:layout_marginLeft="50dip"
303 android:layout_marginRight="30dip"
304 android:text="记住密码"
305 android:button="@drawable/checkbox_icon_no" />"306
308 android:layout_width="wrap_content"
309 android:layout_height="wrap_content"
310 android:text="自动登录"
311 android:paddingRight="50dip"
312 android:button="@drawable/checkbox_icon_no"/>
313
314
315
317 android:layout_height="wrap_content"
318 android:layout_gravity="center_horizontal"
319 android:layout_marginTop="20dip">
320
322 android:layout_width="wrap_content"
323 android:layout_height="wrap_content"
324 android:text="登录"
325 android:layout_marginLeft="50dip"
326 android:textColor="#F7FBFD"
327 android:background="#FF0000"
328 android:width="70dip"
329 android:height="40dip"
330 android:textSize="18dip"
331 />
332
333
335 android:layout_width="wrap_content"
336 android:layout_height="wrap_content"
337 android:text="注册"
338 android:layout_marginLeft="50dip"
339 android:textColor="#F7FBFD"
340 android:width="70dip"
341 android:height="40dip"
342 android:background="#0F9000"
343 android:textSize="18dip"
344 />
345
346
347
348
349
350 登录页面Activity:351
352 packagecom.example.foreveross.office;353
354 importjava.io.IOException;355 importjava.io.UnsupportedEncodingException;356 importjava.util.ArrayList;357 importjava.util.List;358
359 importorg.apache.http.HttpEntity;360 importorg.apache.http.HttpResponse;361 importorg.apache.http.NameValuePair;362 importorg.apache.http.ParseException;363 importorg.apache.http.client.ClientProtocolException;364 importorg.apache.http.client.HttpClient;365 importorg.apache.http.client.entity.UrlEncodedFormEntity;366 importorg.apache.http.client.methods.HttpPost;367 importorg.apache.http.impl.client.DefaultHttpClient;368 importorg.apache.http.message.BasicNameValuePair;369 importorg.apache.http.util.EntityUtils;370
371 importcom.example.wenandroid.R;372 importandroid.app.Activity;373 importandroid.content.Intent;374 importandroid.os.Bundle;375 importandroid.os.StrictMode;376 importandroid.view.View;377 importandroid.view.View.OnClickListener;378 importandroid.view.View.OnFocusChangeListener;379 importandroid.widget.Button;380 importandroid.widget.EditText;381 importandroid.widget.Toast;382
383 public class UserLogin extends Activity implementsOnClickListener {384 privateEditText login_username;385 privateEditText login_password;386 privateButton user_login_button;387 privateButton user_register_button;388
389 @Override390 protected voidonCreate(Bundle savedInstanceState) {391 super.onCreate(savedInstanceState);392 StrictMode.ThreadPolicy policy = newStrictMode.ThreadPolicy.Builder().permitAll().build();393 StrictMode.setThreadPolicy(policy);394 setContentView(R.layout.user_login);395 initWidget();396
397 }398 private voidinitWidget()399 {400 login_username=(EditText)findViewById(R.id.login_username);401 login_password=(EditText)findViewById(R.id.login_password);402 user_login_button=(Button)findViewById(R.id.user_login_button);403 user_register_button=(Button)findViewById(R.id.user_register_button);404 user_login_button.setOnClickListener(this);405 user_register_button.setOnClickListener(this);406 login_username.setOnFocusChangeListener(newOnFocusChangeListener()407 {408
409 @Override410 public void onFocusChange(View v, booleanhasFocus) {411 //TODO Auto-generated method stub
412 if(!hasFocus){413 String username=login_username.getText().toString().trim();414 if(username.length()<4){415 Toast.makeText(UserLogin.this, "用户名不能小于4个字符", Toast.LENGTH_SHORT);416 }417 }418 }419
420 });421 login_password.setOnFocusChangeListener(newOnFocusChangeListener()422 {423
424 @Override425 public void onFocusChange(View v, booleanhasFocus) {426 //TODO Auto-generated method stub
427 if(!hasFocus){428 String password=login_password.getText().toString().trim();429 if(password.length()<4){430 Toast.makeText(UserLogin.this, "密码不能小于4个字符", Toast.LENGTH_SHORT);431 }432 }433 }434
435 });436 }437
438
439 @Override440 public voidonClick(View v) {441 //TODO Auto-generated method stub
442 switch(v.getId())443 {444 caseR.id.user_login_button:445 if(checkEdit())446 {447 login();448 }449
450 break;451 caseR.id.user_register_button:452 Intent intent2=new Intent(UserLogin.this,UserRegister.class);453 startActivity(intent2);454 break;455 }456 }457
458 private booleancheckEdit(){459 if(login_username.getText().toString().trim().equals("")){460 Toast.makeText(UserLogin.this, "用户名不能为空", Toast.LENGTH_SHORT).show();461 }else if(login_password.getText().toString().trim().equals("")){462 Toast.makeText(UserLogin.this, "密码不能为空", Toast.LENGTH_SHORT).show();463 }else{464 return true;465 }466 return false;467 }468
469 private voidlogin(){470 String httpUrl="http://192.168.1.102:8080/web-test/login.jsp";471 HttpPost httpRequest=newHttpPost(httpUrl);472 List params=new ArrayList();473 params.add(new BasicNameValuePair("username",login_username.getText().toString().trim()));474 params.add(new BasicNameValuePair("password",login_password.getText().toString().trim()));475 HttpEntity httpentity = null;476 try{477 httpentity = new UrlEncodedFormEntity(params,"utf8");478 } catch(UnsupportedEncodingException e) {479 //TODO Auto-generated catch block
480 e.printStackTrace();481 }482 httpRequest.setEntity(httpentity);483 HttpClient httpclient=newDefaultHttpClient();484 HttpResponse httpResponse = null;485 try{486 httpResponse =httpclient.execute(httpRequest);487 } catch(ClientProtocolException e) {488 //TODO Auto-generated catch block
489 e.printStackTrace();490 } catch(IOException e) {491 //TODO Auto-generated catch block
492 e.printStackTrace();493 }494 if(httpResponse.getStatusLine().getStatusCode()==200)495 {496 String strResult = null;497 try{498 strResult =EntityUtils.toString(httpResponse.getEntity());499 } catch(ParseException e) {500 //TODO Auto-generated catch block
501 e.printStackTrace();502 } catch(IOException e) {503 //TODO Auto-generated catch block
504 e.printStackTrace();505 }506 Toast.makeText(UserLogin.this, strResult, Toast.LENGTH_SHORT).show();507 Intent intent=new Intent(UserLogin.this,IndexActivity.class);508 startActivity(intent);509 }510 else
511 {512 Toast.makeText(UserLogin.this, "登录失败!", Toast.LENGTH_SHORT).show();513 }514
515 }516 }