1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" 6 android:background="@drawable/bg_01">" 7 8 <TextView 9 android:layout_width="wrap_content" 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 <EditText 21 android:id="@+id/register_username" 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 <EditText 33 android:id="@+id/register_passwd" 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 <EditText 45 android:id="@+id/reregister_passwd" 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 <Button 56 android:id="@+id/register_submit" 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 </LinearLayout> 70 71 处理注册页面的Activity: 72 73 package com.example.foreveross.office; 74 75 import java.io.IOException; 76 import java.io.UnsupportedEncodingException; 77 import java.util.ArrayList; 78 import java.util.List; 79 80 import org.apache.http.HttpEntity; 81 import org.apache.http.HttpResponse; 82 import org.apache.http.NameValuePair; 83 import org.apache.http.ParseException; 84 import org.apache.http.client.ClientProtocolException; 85 import org.apache.http.client.HttpClient; 86 import org.apache.http.client.entity.UrlEncodedFormEntity; 87 import org.apache.http.client.methods.HttpPost; 88 import org.apache.http.impl.client.DefaultHttpClient; 89 import org.apache.http.message.BasicNameValuePair; 90 import org.apache.http.util.EntityUtils; 91 92 import com.example.wenandroid.R; 93 import android.app.Activity; 94 import android.os.Bundle; 95 import android.os.StrictMode; 96 import android.view.View; 97 import android.view.View.OnClickListener; 98 import android.view.View.OnFocusChangeListener; 99 import android.widget.Button; 100 import android.widget.EditText; 101 import android.widget.Toast; 102 103 public class UserRegister extends Activity { 104 105 private EditText register_username; 106 private EditText register_passwd; 107 private EditText reregister_passwd; 108 private Button register_submit; 109 @Override 110 protected void onCreate(Bundle savedInstanceState) { 111 // TODO Auto-generated method stub 112 super.onCreate(savedInstanceState); 113 StrictMode.ThreadPolicy policy = new StrictMode.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(new OnFocusChangeListener() 121 { 122 123 @Override 124 public void onFocusChange(View v, boolean hasFocus) { 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(new OnFocusChangeListener() 135 { 136 137 @Override 138 public void onFocusChange(View v, boolean hasFocus) { 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(new OnFocusChangeListener() 149 { 150 151 @Override 152 public void onFocusChange(View v, boolean hasFocus) { 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(new OnClickListener(){ 163 164 @Override 165 public void onClick(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=new HttpPost(httpUrl); 173 List<NameValuePair> params=new ArrayList<NameValuePair>(); 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=new DefaultHttpClient(); 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 boolean checkEdit(){ 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 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 240 android:layout_width="fill_parent" 241 android:layout_height="fill_parent" 242 android:orientation="vertical" 243 android:background="@drawable/bg_01"> 244 245 <TextView 246 android:layout_width="wrap_content" 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 <LinearLayout 258 android:layout_width="fill_parent" 259 android:layout_height="wrap_content" 260 android:orientation="vertical" > 261 262 <EditText 263 android:id="@+id/login_username" 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 </EditText> 275 276 <EditText 277 android:id="@+id/login_password" 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 </EditText> 290 </LinearLayout> 291 292 <LinearLayout 293 android:layout_width="fill_parent" 294 android:layout_height="wrap_content" 295 android:layout_gravity="center_horizontal" 296 android:layout_marginTop="15dip"> 297 298 <CheckBox 299 android:id="@+id/cb1" 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 <CheckBox 307 android:id="@+id/cb2" 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 </LinearLayout> 314 315 <LinearLayout 316 android:layout_width="fill_parent" 317 android:layout_height="wrap_content" 318 android:layout_gravity="center_horizontal" 319 android:layout_marginTop="20dip"> 320 <Button 321 android:id="@+id/user_login_button" 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 <Button 334 android:id="@+id/user_register_button" 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 </LinearLayout> 347 348 </LinearLayout> 349 350 登录页面Activity: 351 352 package com.example.foreveross.office; 353 354 import java.io.IOException; 355 import java.io.UnsupportedEncodingException; 356 import java.util.ArrayList; 357 import java.util.List; 358 359 import org.apache.http.HttpEntity; 360 import org.apache.http.HttpResponse; 361 import org.apache.http.NameValuePair; 362 import org.apache.http.ParseException; 363 import org.apache.http.client.ClientProtocolException; 364 import org.apache.http.client.HttpClient; 365 import org.apache.http.client.entity.UrlEncodedFormEntity; 366 import org.apache.http.client.methods.HttpPost; 367 import org.apache.http.impl.client.DefaultHttpClient; 368 import org.apache.http.message.BasicNameValuePair; 369 import org.apache.http.util.EntityUtils; 370 371 import com.example.wenandroid.R; 372 import android.app.Activity; 373 import android.content.Intent; 374 import android.os.Bundle; 375 import android.os.StrictMode; 376 import android.view.View; 377 import android.view.View.OnClickListener; 378 import android.view.View.OnFocusChangeListener; 379 import android.widget.Button; 380 import android.widget.EditText; 381 import android.widget.Toast; 382 383 public class UserLogin extends Activity implements OnClickListener { 384 private EditText login_username; 385 private EditText login_password; 386 private Button user_login_button; 387 private Button user_register_button; 388 389 @Override 390 protected void onCreate(Bundle savedInstanceState) { 391 super.onCreate(savedInstanceState); 392 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 393 StrictMode.setThreadPolicy(policy); 394 setContentView(R.layout.user_login); 395 initWidget(); 396 397 } 398 private void initWidget() 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(new OnFocusChangeListener() 407 { 408 409 @Override 410 public void onFocusChange(View v, boolean hasFocus) { 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(new OnFocusChangeListener() 422 { 423 424 @Override 425 public void onFocusChange(View v, boolean hasFocus) { 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 @Override 440 public void onClick(View v) { 441 // TODO Auto-generated method stub 442 switch(v.getId()) 443 { 444 case R.id.user_login_button: 445 if(checkEdit()) 446 { 447 login(); 448 } 449 450 break; 451 case R.id.user_register_button: 452 Intent intent2=new Intent(UserLogin.this,UserRegister.class); 453 startActivity(intent2); 454 break; 455 } 456 } 457 458 private boolean checkEdit(){ 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 void login(){ 470 String httpUrl="http://192.168.1.102:8080/web-test/login.jsp"; 471 HttpPost httpRequest=new HttpPost(httpUrl); 472 List<NameValuePair> params=new ArrayList<NameValuePair>(); 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=new DefaultHttpClient(); 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 }