Android開發中實現使用者註冊和登陸的小demo分享
本文例項講述了Android實現登入功能的方法。分享給大家供大家參考,具體如下:
登陸效果: 應用程式判斷當前使用者還未登陸,彈出登陸對話方塊,使用者輸入賬號和密碼資訊後,傳到伺服器驗證,驗證成功後,現實Toast 成功資訊,並轉到其他介面。
註冊效果:使用者如沒有賬號,則點選登陸對話方塊的 “沒有賬號,快速註冊賬號”, 彈出註冊介面,使用者輸入註冊資訊,點選註冊按鈕,註冊成功後,彈出toast資訊”註冊成功”,完成註冊後,轉到其他功能介面。
整個功能大體上分兩塊:登陸對話方塊:輸入登陸資訊,實現登陸功能,轉到註冊介面。註冊對話方塊:輸入註冊資訊,實現註冊功能。
對話方塊介面佈局xml檔案:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "vertical" > < TextView android:id = "@+id/txt_loginerror" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:textColor = "#ff0000" android:text = "輸入的賬號和密碼不正確" android:gravity = "left" android:textAppearance = "?android:attr/textAppearanceMedium" android:visibility = "invisible" /> < TextView android:id = "@+id/username" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:text = "賬號" android:gravity = "left" android:textAppearance = "?android:attr/textAppearanceMedium" /> < EditText android:id = "@+id/txt_username" android:layout_height = "wrap_content" android:layout_width = "fill_parent" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:autoText = "false" android:capitalize = "none" android:gravity = "fill_horizontal" android:textAppearance = "?android:attr/textAppearanceMedium" /> < TextView android:id = "@+id/password" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:textAppearance = "?android:attr/textAppearanceMedium" android:text = "密碼" android:gravity = "left" /> < EditText android:id = "@+id/txt_password" android:layout_height = "wrap_content" android:layout_width = "fill_parent" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:autoText = "false" android:capitalize = "none" android:gravity = "fill_horizontal" android:textAppearance = "?android:attr/textAppearanceMedium" /> < TextView android:id = "@+id/txt_toregister" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:textColor = "#2200C1" android:textAppearance = "?android:attr/textAppearanceMedium" android:text = "沒有賬號?快速註冊" android:gravity = "left" /> </ LinearLayout > |
後臺業務邏輯:
?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | /* * 建立使用者登陸的對話方塊 * 登陸介面包含兩個按鈕 * 1按鈕為登陸 * 2按鈕為不登陸試玩 * */ private void CreateLoginAlert() { AlertDialog.Builder ad =new AlertDialog.Builder(this); ad.setTitle("賬號登陸"); ad.setView(ViewUtility.GetView(this,R.layout.sub_logindialog)); adi= ad.create(); /* */ adi.setButton("登陸", new OnClickListener(){ @Override public void onClick(DialogInterface arg0, int arg1) { EditText password= (EditText)adi.findViewById(R.id.txt_password); EditText account =(EditText)adi.findViewById(R.id.txt_username); PassWord=password.getText().toString(); Account=account.getText().toString(); //生成登陸對話方塊 m_Dialog=ProgressDialog.show(Main.this, "請等待...", "正在為你登陸...",true); mRedrawHandler.sleep(100); } }); adi.setButton2("試 玩", new OnClickListener(){ @Override public void onClick(DialogInterface arg0, int arg1) { ViewUtility.NavigateActivate(Main.this, SelectTheme.class); } }); adi.show(); //設定註冊點選事件 TextView register=(TextView)adi.findViewById(R.id.txt_toregister); register.setOnClickListener(new TextView.OnClickListener() { public void onClick(View v){ //建立註冊對話方塊 CreateRegisterAlert(); adi.dismiss(); } }); } /* *定時執行緒做驗證用 * */ private RefreshHandler mRedrawHandler = new RefreshHandler(); class RefreshHandler extends Handler { @Override public void handleMessage(Message msg) { try { //呼叫網路介面,實現登陸指令 Boolean flags= UserDataServiceHelper.Login(Account, PassWord); if (flags) { //儲存登陸資訊 UserDataWriteHelper uw= new UserDataWriteHelper(Main. this ); uw.SaveUserInfoInDB( "xuwenbing" , Account); //提示登陸成功 Toast.makeText(Main. this , "登陸成功" , Toast.LENGTH_SHORT).show(); //轉到主題頁面 ViewUtility.NavigateActivate(Main. this , SelectTheme. class ); } else { //失敗 顯示錯誤資訊 Toast.makeText(Main. this , "登陸失敗" , Toast.LENGTH_SHORT).show(); adi.show(); adi.findViewById(R.id.txt_loginerror).setVisibility(View.VISIBLE); } } catch (Exception e) { e.printStackTrace(); } finally { m_Dialog.dismiss(); } } public void sleep( long delayMillis) { this .removeMessages( 0 ); sendMessageDelayed(obtainMessage( 0 ), delayMillis); } }; |
對話方塊介面佈局xml檔案:
?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 | <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "vertical" > < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" android:gravity = "center" > < TextView android:id = "@+id/txt_loginerror" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:textColor = "#ff0000" android:text = "輸入的賬號和密碼不正確" android:gravity = "left" android:textAppearance = "?android:attr/textAppearanceMedium" android:visibility = "invisible" /> </ LinearLayout > < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" > < TextView android:id = "@+id/username" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:text = "賬號" android:gravity = "left" android:textAppearance = "?android:attr/textAppearanceMedium" /> < EditText android:id = "@+id/txt_username" android:layout_height = "wrap_content" android:layout_width = "fill_parent" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:autoText = "false" android:capitalize = "none" android:gravity = "fill_horizontal" android:textAppearance = "?android:attr/textAppearanceMedium" /> </ LinearLayout > < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" > < TextView android:id = "@+id/password" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:textAppearance = "?android:attr/textAppearanceMedium" android:text = "密碼" android:gravity = "left" /> < EditText android:id = "@+id/txt_password" android:layout_height = "wrap_content" android:layout_width = "fill_parent" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:autoText = "false" android:capitalize = "none" android:gravity = "fill_horizontal" android:textAppearance = "?android:attr/textAppearanceMedium" /> </ LinearLayout > < LinearLayout android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:orientation = "horizontal" > < TextView android:id = "@+id/nicename" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:textAppearance = "?android:attr/textAppearanceMedium" android:text = "暱稱" android:gravity = "left" /> < EditText android:id = "@+id/txt_nicename" android:layout_height = "wrap_content" android:layout_width = "fill_parent" android:layout_marginLeft = "20dip" android:layout_marginRight = "20dip" android:autoText = "false" android:capitalize = "none" android:gravity = "fill_horizontal" android:textAppearance = "?android:attr/textAppearanceMedium" /> </ LinearLayout > </ LinearLayout > |
後臺業務邏輯:
?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 | /*建立註冊對話方塊*/ private void CreateRegisterAlert() { //registerdialog AlertDialog.Builder ad =new AlertDialog.Builder(this); ad.setTitle("註冊賬號"); ad.setView(ViewUtility.GetView(this,R.layout.sub_registerdialog)); registerdialog= ad.create(); registerdialog.setButton("註冊", new OnClickListener(){ @Override public void onClick(DialogInterface arg0, int arg1) { EditText password= (EditText)registerdialog.findViewById(R.id.txt_password); EditText account =(EditText)registerdialog.findViewById(R.id.txt_username); EditText nicename =(EditText)registerdialog.findViewById(R.id.txt_nicename); PassWord=password.getText().toString(); Account=account.getText().toString(); NiceName=nicename.getText().toString(); //生成註冊對話方塊 m_Dialog=ProgressDialog.show(Main.this, "請等待...", "正在為你註冊...",true); mRegsiterHandler.sleep(100); } }); registerdialog.setButton2("試 玩", new OnClickListener(){ @Override public void onClick(DialogInterface arg0, int arg1) { ViewUtility.NavigateActivate(Main.this, SelectTheme.class); } }); registerdialog.show(); } /* *定時註冊程式 * */ private RegsiterHandler mRegsiterHandler = new RegsiterHandler(); class RegsiterHandler extends Handler { @Override public void handleMessage(Message msg) { try { //呼叫網路介面,實現註冊指令 Boolean flags= UserDataServiceHelper.Register(Account, PassWord,NiceName); if (flags) { //儲存註冊資訊 UserDataWriteHelper uw= new UserDataWriteHelper(Main. this ); uw.SaveUserInfoInDB( "xuwenbing" , Account);
|