这期内容当中小编将会给大家带来有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。