您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Android中怎么利用系統對話框實現登陸注冊功能,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
界面布局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="輸入的賬號和密碼不正確" 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" 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="沒有賬號?快速注冊" android:gravity="left" /> </LinearLayout>
2.2 后臺業務邏輯
/* * 創建用戶登陸的對話框 * 登陸界面包含兩個按鈕 * 1按鈕為登陸 * 2按鈕為不登陸試玩 * */ private void CreateLoginAlert() { AlertDialog.Builder ad =new AlertDialog.Builder(this); ad.setTitle("賬號登陸"); 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(); //設置注冊點擊事件 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); } };
3 注冊對話框的實現
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="輸入的賬號和密碼不正確" 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>
3.2 后臺業務邏輯
/*創建注冊對話框*/ 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); //提示注冊成功 Toast.makeText(Main.this, "注冊成功", Toast.LENGTH_SHORT).show(); //轉到主題頁面 ViewUtility.NavigateActivate(Main.this, SelectTheme.class); }else { //失敗 顯示錯誤信息 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中怎么利用系統對話框實現登陸注冊功能了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。