Android實(shí)戰(zhàn)系統(tǒng)對話框?qū)崿F(xiàn)登陸注冊功能
Android實(shí)例呈現(xiàn)效果和實(shí)現(xiàn)說明
下面是實(shí)現(xiàn)的效果圖
實(shí)現(xiàn)的效果說明:
登陸效果: 應(yīng)用程序判斷當(dāng)前用戶還未登陸,彈出登陸對話框,用戶輸入賬號(hào)和密碼信息后,傳到服務(wù)器驗(yàn)證,驗(yàn)證成功后,現(xiàn)實(shí)Toast 成功信息,并轉(zhuǎn)到其他界面
注冊效果:用戶如沒有賬號(hào),則點(diǎn)擊登陸對話框的 “沒有賬號(hào),快速注冊賬號(hào)”
彈出注冊界面,用戶輸入注冊信息,點(diǎn)擊注冊按鈕,注冊成功后,彈出toast信息”注冊成功”,完成注冊后,轉(zhuǎn)到其他功能界面。
功能實(shí)現(xiàn)說明:
整個(gè)功能大體上分兩塊
登陸對話框:輸入登陸信息,實(shí)現(xiàn)登陸功能,轉(zhuǎn)到注冊界面
注冊對話框:輸入注冊信息,實(shí)現(xiàn)注冊功能
#p#
2 登陸對話框的實(shí)現(xiàn)
2.1 對話框界面布局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="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="輸入的賬號(hào)和密碼不正確"
- 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="賬號(hào)"
- 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"
- androidandroid: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="沒有賬號(hào)?快速注冊"
- android:gravity="left"
- />
- </LinearLayout>
#p#
2.2 后臺(tái)業(yè)務(wù)邏輯
- /*
- * 創(chuàng)建用戶登陸的對話框
- * 登陸界面包含兩個(gè)按鈕
- * 1按鈕為登陸
- * 2按鈕為不登陸試玩
- * */
- private void CreateLoginAlert()
- {
- AlertDialog.Builder ad =new AlertDialog.Builder(this);
- ad.setTitle("賬號(hào)登陸");
- ad.setView(ViewUtility.GetView(this,R.layout.sub_logindialog));
- adadi= 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();
- //設(shè)置注冊點(diǎn)擊事件
- TextView register=(TextView)adi.findViewById(R.id.txt_toregister);
- register.setOnClickListener(new TextView.OnClickListener()
- {
- public void onClick(View v){
- //創(chuàng)建注冊對話框
- CreateRegisterAlert();
- adi.dismiss();
- }
- });
- }
- /*
- *定時(shí)線程做驗(yàn)證用
- * */
- private RefreshHandler mRedrawHandler = new RefreshHandler();
- class RefreshHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- try{
- //調(diào)用網(wǎng)絡(luò)接口,實(shí)現(xiàn)登陸指令
- 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();
- //轉(zhuǎn)到主題頁面
- ViewUtility.NavigateActivate(Main.this, SelectTheme.class);
- }else
- {
- //失敗 顯示錯(cuò)誤信息
- 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);
- }
- };
#p#
3 注冊對話框的實(shí)現(xiàn)
3.1 對話框界面布局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="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="輸入的賬號(hào)和密碼不正確"
- 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="賬號(hào)"
- 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>
#p#
3.2 后臺(tái)業(yè)務(wù)邏輯
- /*創(chuàng)建注冊對話框*/
- private void CreateRegisterAlert()
- {
- //registerdialog
- AlertDialog.Builder ad =new AlertDialog.Builder(this);
- ad.setTitle("注冊賬號(hào)");
- 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();
- }
- /*
- *定時(shí)注冊程序
- * */
- private RegsiterHandler mRegsiterHandler = new RegsiterHandler();
- class RegsiterHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- try{
- //調(diào)用網(wǎng)絡(luò)接口,實(shí)現(xiàn)注冊指令
- Boolean flags= UserDataServiceHelper.Register(Account, PassWord,NiceName);
- if(flags)
- {
- //保存注冊信息
- UserDataWriteHelper uw=new UserDataWriteHelper(Main.this);
- uw.SaveUserInfoInDB("xuwenbing", Account);
- //提示注冊成功
- Toast.makeText(Main.this, "注冊成功", Toast.LENGTH_SHORT).show();
- //轉(zhuǎn)到主題頁面
- ViewUtility.NavigateActivate(Main.this, SelectTheme.class);
- }else
- {
- //失敗 顯示錯(cuò)誤信息
- Toast.makeText(Main.this, "注冊失敗", Toast.LENGTH_SHORT).show();
- registerdialog.show();
- registerdialog.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);
- }
- };
【編輯推薦】
谷歌Android UI設(shè)計(jì)技巧:框架特性