<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: background = "@ drawable/bg_01"> "<TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" register "android: textSize = "22dip" android: textColor = "# FFFFFF" android: paddingLeft = "140dip" android: paddingRight = "50dip" android: paddingTop = "10dip" android: background = "@ drawable/topbg"/> "<EditText android: id =" @ + id/register_username "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_marginTop = "20dip" android: background = "@ drawable/search" android: layout_marginLeft = "20dip" android: layout_marginRight = "20dip" android: height = "40dip" android: hint = "username"/> <EditText android: id = "@ + id/register_passwd" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "20dip" android: background = "@ drawable/search" android: layout_marginLeft = "20dip" android: layout_marginRight = "20dip" android: height = "40dip" android: hint = "password"/> <EditText android: id = "@ + id/reregister_passwd" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginTop = "20dip" android: background = "@ drawable/search" android: layout_marginLeft = "20dip" android: layout_marginRight = "20dip" android: height = "40dip" android: hint = "Confirm Password"/> <Button android: id = "@ + id/register_submit" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: background = "@ drawable/topbg" android: height = "40dip" android: width = "70dip" android: layout_marginTop = "60dip" android: text = "OK" android: textColor = "# FFFFFF" android: textSize = "22dip"/> </LinearLayout>
Process the Activity on the registration page:
Package com. example. foreveross. office; import java. io. IOException; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. list; import org. apache. http. httpEntity; import org. apache. http. httpResponse; import org. apache. http. nameValuePair; import org. apache. http. parseException; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; imp Ort org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpPost; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. message. basicNameValuePair; import org. apache. http. util. entityUtils; import com. example. wenandroid. r; import android. app. activity; import android. OS. bundle; import android. OS. strictMode; import android. view. view; import android. v Iew. view. onClickListener; import android. view. view. onFocusChangeListener; import android. widget. button; import android. widget. editText; import android. widget. toast; public class UserRegister extends Activity {private EditText register_username; private EditText register_passwd; private EditText success; private Button register_submit; @ Overrideprotected void onCreate (Bundle savedInstanceState ){ // TODO Auto-generated method stubsuper. onCreate (savedInstanceState); StrictMode. threadPolicy policy = new StrictMode. threadPolicy. builder (). permitAll (). build (); StrictMode. setThreadPolicy (policy); setContentView (R. layout. user_register); register_username = (EditText) findViewById (R. id. register_username); register_passwd = (EditText) findViewById (R. id. register_passwd); reregister_passwd = (EditText) findViewById (R. id. reregister_passwd); register_submit = (Button) findViewById (R. id. register_submit); register_username.setOnFocusChangeListener (new OnFocusChangeListener () {@ Overridepublic void onFocusChange (View v, boolean hasFocus) {// TODO Auto-generated method stubif (! HasFocus) {if (register_username.getText (). toString (). trim (). length () <4) {Toast. makeText (UserRegister. this, "the user name cannot be less than 4 characters", Toast. LENGTH_SHORT ). show () ;}}}); listener (new OnFocusChangeListener () {@ Overridepublic void onFocusChange (View v, boolean hasFocus) {// TODO Auto-generated method stubif (! HasFocus) {if (register_passwd.getText (). toString (). trim (). length () <6) {Toast. makeText (UserRegister. this, "the password cannot be less than 8 characters", Toast. LENGTH_SHORT ). show () ;}}}); listener (new OnFocusChangeListener () {@ Overridepublic void onFocusChange (View v, boolean hasFocus) {// TODO Auto-generated method stubif (! HasFocus) {if (! Reregister_passwd.getText (). toString (). trim (). equals (register_passwd.getText (). toString (). trim () {Toast. makeText (UserRegister. this, "The two passwords are inconsistent", Toast. LENGTH_SHORT ). show () ;}}}); register_submit.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {if (! CheckEdit () {return;} // TODO Auto-generated method stubString httpUrl = "http: // 192.168.1.100: 8080/web-test/register. jsp "; HttpPost httpRequest = new HttpPost (httpUrl); List <NameValuePair> params = new ArrayList <NameValuePair> (); params. add (new BasicNameValuePair ("username", register_username.getText (). toString (). trim (); params. add (new BasicNameValuePair ("password", register_passwd.getText (). toString (). trim (); HttpEntity httpentity = null; try {httpentity = new UrlEncodedFormEntity (params, "utf8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke. printStackTrace ();} httpRequest. setEntity (httpentity); HttpClient httpclient = new entity (); HttpResponse httpResponse = null; try {httpResponse = httpclient.exe cute (httpRequest);} catch (entity e) {// TODO Au To-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} if (httpResponse. getStatusLine (). getStatusCode () == 200) {String strResult = null; try {strResult = EntityUtils. toString (httpResponse. getEntity ();} catch (ParseException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catc H blocke. printStackTrace ();} Toast. makeText (UserRegister. this, strResult, Toast. LENGTH_SHORT ). show ();} else {Toast. makeText (UserRegister. this, "request error", Toast. LENGTH_SHORT ). show () ;}}) ;}private boolean checkEdit () {if (register_username.getText (). toString (). trim (). equals ("") {Toast. makeText (UserRegister. this, "the user name cannot be blank", Toast. LENGTH_SHORT ). show ();} else if (register_passwd.getText (). toString (). trim (). equal S ("") {Toast. makeText (UserRegister. this, "the password cannot be blank", Toast. LENGTH_SHORT). show ();} else if (! Register_passwd.getText (). toString (). trim (). equals (reregister_passwd.getText (). toString (). trim () {Toast. makeText (UserRegister. this, "The two passwords are inconsistent", Toast. LENGTH_SHORT ). show () ;}else {return true;} return false ;}}
Login Page xml:
User_login.xml:
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical" android: background = "@ drawable/bg_01"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "login" android: textSize = "22dip" android: textColor = "# FFFFFF" android: paddingLeft = "140dip" android: paddingRight = "50dip" android: paddingTop = "10dip" android: background = "@ drawable/topbg"/> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: orientation = "vertical"> <EditText android: id = "@ + id/login_username" android: layout_width = "fill_parent" android: layout_height = "40dip" android: layout_marginLeft = "20dip" android: layout_marginRight = "20dip" android: layout_marginTop = "30dip" android: hint = "username" android: paddingTop = "10dip" android: textSize = "18dip" android: background = "@ drawable/search"> </EditText> <EditText android: id = "@ + id/login_password" android: layout_width = "fill_parent" android: layout_height = "40dip" android: layout_marginLeft = "20dip" android: layout_marginRight = "20dip" android: layout_marginTop = "10dip" android: password = "true" android: paddingTop = "10dip" android: textSize = "18dip" android: hint = "password" android: background = "@ drawable/search"> </EditText> </LinearLayout> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_gravity = "center_horizontal" android: layout_marginTop = "15dip"> <CheckBox android: id = "@ + id/cb1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginLeft = "50dip" android: layout_marginRight = "30dip" android: text = "Remember password" android: button = "@ drawable/checkbox_icon_no"/> "<CheckBox android: id =" @ + id/cb2 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "Automatic Logon" android: paddingRight = "50dip" android: button = "@ drawable/checkbox_icon_no"/> </LinearLayout> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: layout_gravity = "center_horizontal" android: layout_marginTop = "20dip"> <Button android: id = "@ + id/user_login_button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "login" android: layout_marginLeft = "50dip" android: textColor = "# F7FBFD" android: background = "# FF0000" android: width = "70dip" android: height = "40dip" android: textSize = "18dip"/> <Button android: id = "@ + id/user_register_button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "registered" android: layout_marginLeft = "50dip" android: textColor = "# F7FBFD" android: width = "70dip" android: height = "40dip" android: background = "#0F9000" android: textSize = "18dip"/> </LinearLayout>
Logon page Activity:
Package com. example. foreveross. office; import java. io. IOException; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. list; import org. apache. http. httpEntity; import org. apache. http. httpResponse; import org. apache. http. nameValuePair; import org. apache. http. parseException; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; imp Ort org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpPost; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. message. basicNameValuePair; import org. apache. http. util. entityUtils; import com. example. wenandroid. r; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. OS. strictMode; import android Oid. view. view; import android. view. view. onClickListener; import android. view. view. onFocusChangeListener; import android. widget. button; import android. widget. editText; import android. widget. toast; public class UserLogin extends Activity implements OnClickListener {private EditText login_username; private EditText login_password; private Button user_login_button; private Button user_register_button; @ Override Protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); StrictMode. threadPolicy policy = new StrictMode. threadPolicy. builder (). permitAll (). build (); StrictMode. setThreadPolicy (policy); setContentView (R. layout. user_login); initWidget ();} private void initWidget () {login_username = (EditText) findViewById (R. id. login_username); login_password = (EditText) findViewById (R. id. login_pas Sword); user_login_button = (Button) findViewById (R. id. user_login_button); user_register_button = (Button) findViewById (R. id. user_register_button); user_login_button.setOnClickListener (this); listener (new OnFocusChangeListener () {@ Overridepublic void onFocusChange (View v, boolean hasFocus) {// TODO Auto-generated method stubif (! HasFocus) {String username = login_username.getText (). toString (). trim (); if (username. length () <4) {Toast. makeText (UserLogin. this, "the user name cannot be less than 4 characters", Toast. LENGTH_SHORT) ;}}}); listener (new OnFocusChangeListener () {@ Overridepublic void onFocusChange (View v, boolean hasFocus) {// TODO Auto-generated method stubif (! HasFocus) {String password = login_password.getText (). toString (). trim (); if (password. length () <4) {Toast. makeText (UserLogin. this, "the password cannot be less than 4 characters", Toast. LENGTH_SHORT) ;}}}) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. user_login_button: if (checkEdit () {login () ;}break; case R. id. user_register_button: Intent intent2 = new Intent (UserLogin. this, UserRegister. cl Ass); startActivity (intent2); break;} private boolean checkEdit () {if (login_username.getText (). toString (). trim (). equals ("") {Toast. makeText (UserLogin. this, "the user name cannot be blank", Toast. LENGTH_SHORT ). show ();} else if (login_password.getText (). toString (). trim (). equals ("") {Toast. makeText (UserLogin. this, "the password cannot be blank", Toast. LENGTH_SHORT ). show () ;}else {return true;} return false;} private void login () {String httpUrl = "http :// 192.168.1.102: 8080/web-test/login. jsp "; HttpPost httpRequest = new HttpPost (httpUrl); List <NameValuePair> params = new ArrayList <NameValuePair> (); params. add (new BasicNameValuePair ("username", login_username.getText (). toString (). trim (); params. add (new BasicNameValuePair ("password", login_password.getText (). toString (). trim (); HttpEntity httpentity = null; try {httpentity = new UrlEncodedFormEntity (params ," Utf8 ");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke. printStackTrace ();} httpRequest. setEntity (httpentity); HttpClient httpclient = new response (); HttpResponse httpResponse = null; try {httpResponse = httpclient.exe cute (httpRequest);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-ge Nerated catch blocke. printStackTrace ();} if (httpResponse. getStatusLine (). getStatusCode () == 200) {String strResult = null; try {strResult = EntityUtils. toString (httpResponse. getEntity ();} catch (ParseException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} Toast. makeText (UserLogin. this, strResult, Toast. LENGTH_S HORT ). show (); Intent intent = new Intent (UserLogin. this, IndexActivity. class); startActivity (intent);} else {Toast. makeText (UserLogin. this, "Logon Failed! ", Toast. LENGTH_SHORT). show ();}}}