Android在对话框中实现登录与注册
来源:互联网 发布:java 表达式引擎 编辑:程序博客网 时间:2024/05/21 08:58
Android在对话框中实现登录与注册
2011-05-29
在android的应用中越来越多的包含了网络互动功能,这就带来了注册,登陆账号功能。本文完整的介绍对话框的方式实现用户登陆功能。
登陆效果: 应用程序判断当前用户还未登陆,弹出登陆对话框,用户输入账号和密码信息后,传到服务器验证,验证成功后,现实Toast 成功信息,并转到其他界面。
注册效果:用户如没有账号,则点击登陆对话框的 "没有账号,快速注册账号", 弹出注册界面,用户输入注册信息,点击注册按钮,注册成功后,弹出toast信息"注册成功",完成注册后,转到其他功能界面。
整个功能大体上分两块:登陆对话框:输入登陆信息,实现登陆功能,转到注册界面。注册对话框:输入注册信息,实现注册功能。
对话框界面布局xml文件:
01
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02
<
LinearLayout
03
xmlns:android
=
"http://schemas.android.com/apk/res/android"
04
android:layout_width
=
"fill_parent"
05
android:layout_height
=
"wrap_content"
06
android:orientation
=
"vertical"
>
07
08
<
TextView
09
android:id
=
"@+id/txt_loginerror"
10
android:layout_height
=
"wrap_content"
11
android:layout_width
=
"wrap_content"
12
android:layout_marginLeft
=
"20dip"
13
android:layout_marginRight
=
"20dip"
14
android:textColor
=
"#ff0000"
15
android:text
=
"输入的账号和密码不正确"
16
android:gravity
=
"left"
17
android:textAppearance
=
"?android:attr/textAppearanceMedium"
18
android:visibility
=
"invisible"
19
/>
20
21
22
<
TextView
23
android:id
=
"@+id/username"
24
android:layout_height
=
"wrap_content"
25
android:layout_width
=
"wrap_content"
26
android:layout_marginLeft
=
"20dip"
27
android:layout_marginRight
=
"20dip"
28
android:text
=
"账号"
29
android:gravity
=
"left"
30
android:textAppearance
=
"?android:attr/textAppearanceMedium"
31
/>
32
33
<
EditText
34
android:id
=
"@+id/txt_username"
35
android:layout_height
=
"wrap_content"
36
android:layout_width
=
"fill_parent"
37
android:layout_marginLeft
=
"20dip"
38
android:layout_marginRight
=
"20dip"
39
android:autoText
=
"false"
40
android:capitalize
=
"none"
41
android:gravity
=
"fill_horizontal"
42
android:textAppearance
=
"?android:attr/textAppearanceMedium"
43
/>
44
<
TextView
45
android:id
=
"@+id/password"
46
android:layout_height
=
"wrap_content"
47
android:layout_width
=
"wrap_content"
48
android:layout_marginLeft
=
"20dip"
49
android:layout_marginRight
=
"20dip"
50
android:textAppearance
=
"?android:attr/textAppearanceMedium"
51
android:text
=
"密码"
52
android:gravity
=
"left"
53
/>
54
<
EditText
55
android:id
=
"@+id/txt_password"
56
android:layout_height
=
"wrap_content"
57
android:layout_width
=
"fill_parent"
58
android:layout_marginLeft
=
"20dip"
59
android:layout_marginRight
=
"20dip"
60
android:autoText
=
"false"
61
android:capitalize
=
"none"
62
android:gravity
=
"fill_horizontal"
63
android:textAppearance
=
"?android:attr/textAppearanceMedium"
64
/>
65
66
<
TextView
67
android:id
=
"@+id/txt_toregister"
68
android:layout_height
=
"wrap_content"
69
android:layout_width
=
"wrap_content"
70
android:layout_marginLeft
=
"20dip"
71
android:layout_marginRight
=
"20dip"
72
android:textColor
=
"#2200C1"
73
android:textAppearance
=
"?android:attr/textAppearanceMedium"
74
android:text
=
"没有账号?快速注册"
75
android:gravity
=
"left"
76
/>
77
78
</
LinearLayout
>
后台业务逻辑:
001
/*
002
* 创建用户登陆的对话框
003
* 登陆界面包含两个按钮
004
* 1按钮为登陆
005
* 2按钮为不登陆试玩
006
* */
007
private void CreateLoginAlert()
008
{
009
AlertDialog.Builder ad =new AlertDialog.Builder(this);
010
ad.setTitle("账号登陆");
011
ad.setView(ViewUtility.GetView(this,R.layout.sub_logindialog));
012
adi= ad.create();
013
014
015
/*
016
*/
017
adi.setButton("登陆", new OnClickListener(){
018
019
@Override
020
public void onClick(DialogInterface arg0, int arg1) {
021
022
EditText password= (EditText)adi.findViewById(R.id.txt_password);
023
EditText account =(EditText)adi.findViewById(R.id.txt_username);
024
025
PassWord=password.getText().toString();
026
Account=account.getText().toString();
027
//生成登陆对话框
028
m_Dialog=ProgressDialog.show(Main.this, "请等待...", "正在为你登陆...",true);
029
030
mRedrawHandler.sleep(100);
031
}
032
});
033
034
adi.setButton2("试 玩", new OnClickListener(){
035
036
@Override
037
public void onClick(DialogInterface arg0, int arg1) {
038
ViewUtility.NavigateActivate(Main.this, SelectTheme.class);
039
}
040
});
041
042
adi.show();
043
044
045
//设置注册点击事件
046
TextView register=(TextView)adi.findViewById(R.id.txt_toregister);
047
register.setOnClickListener(new TextView.OnClickListener()
048
{
049
public void onClick(View v){
050
//创建注册对话框
051
CreateRegisterAlert();
052
adi.dismiss();
053
054
}
055
});
056
057
}
058
059
/*
060
*定时线程做验证用
061
* */
062
private
RefreshHandler mRedrawHandler =
new
RefreshHandler();
063
064
class
RefreshHandler
extends
Handler {
065
066
@Override
067
public
void
handleMessage(Message msg) {
068
069
try
{
070
071
//调用网络接口,实现登陆指令
072
Boolean flags= UserDataServiceHelper.Login(Account, PassWord);
073
if
(flags)
074
{
075
//保存登陆信息
076
UserDataWriteHelper uw=
new
UserDataWriteHelper(Main.
this
);
077
uw.SaveUserInfoInDB(
"xuwenbing"
, Account);
078
//提示登陆成功
079
Toast.makeText(Main.
this
,
"登陆成功"
, Toast.LENGTH_SHORT).show();
080
//转到主题页面
081
ViewUtility.NavigateActivate(Main.
this
, SelectTheme.
class
);
082
}
else
083
{
084
//失败 显示错误信息
085
Toast.makeText(Main.
this
,
"登陆失败"
, Toast.LENGTH_SHORT).show();
086
adi.show();
087
adi.findViewById(R.id.txt_loginerror).setVisibility(View.VISIBLE);
088
}
089
}
090
catch
(Exception e)
091
{
092
e.printStackTrace();
093
}
094
finally
{
095
m_Dialog.dismiss();
096
}
097
}
098
public
void
sleep(
long
delayMillis) {
099
this
.removeMessages(
0
);
100
sendMessageDelayed(obtainMessage(
0
), delayMillis);
101
}
102
};
对话框界面布局xml文件:
001
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
002
<
LinearLayout
003
xmlns:android
=
"http://schemas.android.com/apk/res/android"
004
android:layout_width
=
"fill_parent"
005
android:layout_height
=
"wrap_content"
006
android:orientation
=
"vertical"
>
007
008
<
LinearLayout
009
android:layout_width
=
"fill_parent"
010
android:layout_height
=
"wrap_content"
011
android:orientation
=
"horizontal"
012
android:gravity
=
"center"
013
>
014
<
TextView
015
android:id
=
"@+id/txt_loginerror"
016
android:layout_height
=
"wrap_content"
017
android:layout_width
=
"wrap_content"
018
android:layout_marginLeft
=
"20dip"
019
android:layout_marginRight
=
"20dip"
020
android:textColor
=
"#ff0000"
021
android:text
=
"输入的账号和密码不正确"
022
android:gravity
=
"left"
023
android:textAppearance
=
"?android:attr/textAppearanceMedium"
024
android:visibility
=
"invisible"
025
/>
026
</
LinearLayout
>
027
028
<
LinearLayout
029
android:layout_width
=
"fill_parent"
030
android:layout_height
=
"wrap_content"
031
android:orientation
=
"horizontal"
032
033
>
034
<
TextView
035
android:id
=
"@+id/username"
036
android:layout_height
=
"wrap_content"
037
android:layout_width
=
"wrap_content"
038
android:layout_marginLeft
=
"20dip"
039
android:layout_marginRight
=
"20dip"
040
android:text
=
"账号"
041
android:gravity
=
"left"
042
android:textAppearance
=
"?android:attr/textAppearanceMedium"
043
/>
044
045
<
EditText
046
android:id
=
"@+id/txt_username"
047
android:layout_height
=
"wrap_content"
048
android:layout_width
=
"fill_parent"
049
android:layout_marginLeft
=
"20dip"
050
android:layout_marginRight
=
"20dip"
051
android:autoText
=
"false"
052
android:capitalize
=
"none"
053
android:gravity
=
"fill_horizontal"
054
android:textAppearance
=
"?android:attr/textAppearanceMedium"
055
/>
056
</
LinearLayout
>
057
058
<
LinearLayout
059
android:layout_width
=
"fill_parent"
060
android:layout_height
=
"wrap_content"
061
android:orientation
=
"horizontal"
062
063
>
064
<
TextView
065
android:id
=
"@+id/password"
066
android:layout_height
=
"wrap_content"
067
android:layout_width
=
"wrap_content"
068
android:layout_marginLeft
=
"20dip"
069
android:layout_marginRight
=
"20dip"
070
android:textAppearance
=
"?android:attr/textAppearanceMedium"
071
android:text
=
"密码"
072
android:gravity
=
"left"
073
/>
074
<
EditText
075
android:id
=
"@+id/txt_password"
076
android:layout_height
=
"wrap_content"
077
android:layout_width
=
"fill_parent"
078
android:layout_marginLeft
=
"20dip"
079
android:layout_marginRight
=
"20dip"
080
android:autoText
=
"false"
081
android:capitalize
=
"none"
082
android:gravity
=
"fill_horizontal"
083
android:textAppearance
=
"?android:attr/textAppearanceMedium"
084
/>
085
</
LinearLayout
>
086
<
LinearLayout
087
android:layout_width
=
"fill_parent"
088
android:layout_height
=
"wrap_content"
089
android:orientation
=
"horizontal"
090
>
091
<
TextView
092
android:id
=
"@+id/nicename"
093
android:layout_height
=
"wrap_content"
094
android:layout_width
=
"wrap_content"
095
android:layout_marginLeft
=
"20dip"
096
android:layout_marginRight
=
"20dip"
097
android:textAppearance
=
"?android:attr/textAppearanceMedium"
098
android:text
=
"昵称"
099
android:gravity
=
"left"
100
/>
101
<
EditText
102
android:id
=
"@+id/txt_nicename"
103
android:layout_height
=
"wrap_content"
104
android:layout_width
=
"fill_parent"
105
android:layout_marginLeft
=
"20dip"
106
android:layout_marginRight
=
"20dip"
107
android:autoText
=
"false"
108
android:capitalize
=
"none"
109
android:gravity
=
"fill_horizontal"
110
android:textAppearance
=
"?android:attr/textAppearanceMedium"
111
/>
112
113
</
LinearLayout
>
114
115
</
LinearLayout
>
后台业务逻辑:
01
/*创建注册对话框*/
02
private void CreateRegisterAlert()
03
{
04
//registerdialog
05
AlertDialog.Builder ad =new AlertDialog.Builder(this);
06
ad.setTitle("注册账号");
07
ad.setView(ViewUtility.GetView(this,R.layout.sub_registerdialog));
08
registerdialog= ad.create();
09
10
registerdialog.setButton("注册", new OnClickListener(){
11
12
@Override
13
public void onClick(DialogInterface arg0, int arg1) {
14
15
EditText password= (EditText)registerdialog.findViewById(R.id.txt_password);
16
EditText account =(EditText)registerdialog.findViewById(R.id.txt_username);
17
EditText nicename =(EditText)registerdialog.findViewById(R.id.txt_nicename);
18
19
PassWord=password.getText().toString();
20
Account=account.getText().toString();
21
NiceName=nicename.getText().toString();
22
//生成注册对话框
23
m_Dialog=ProgressDialog.show(Main.this, "请等待...", "正在为你注册...",true);
24
25
mRegsiterHandler.sleep(100);
26
}
27
});
28
29
registerdialog.setButton2("试 玩", new OnClickListener(){
30
31
@Override
32
public void onClick(DialogInterface arg0, int arg1) {
33
ViewUtility.NavigateActivate(Main.this, SelectTheme.class);
34
}
35
});
36
37
registerdialog.show();
38
}
39
40
/*
41
*定时注册程序
42
* */
43
private
RegsiterHandler mRegsiterHandler =
new
RegsiterHandler();
44
45
class
RegsiterHandler
extends
Handler {
46
47
@Override
48
public
void
handleMessage(Message msg) {
49
50
try
{
51
52
//调用网络接口,实现注册指令
53
Boolean flags= UserDataServiceHelper.Register(Account, PassWord,NiceName);
54
if
(flags)
55
{
56
//保存注册信息
57
UserDataWriteHelper uw=
new
UserDataWriteHelper(Main.
this
);
58
uw.SaveUserInfoInDB(
"xuwenbing"
, Account);
59
//提示注册成功
60
Toast.makeText(Main.
this
,
"注册成功"
, Toast.LENGTH_SHORT).show();
61
//转到主题页面
62
ViewUtility.NavigateActivate(Main.
this
, SelectTheme.
class
);
63
}
else
64
{
65
//失败 显示错误信息
66
Toast.makeText(Main.
this
,
"注册失败"
, Toast.LENGTH_SHORT).show();
67
registerdialog.show();
68
registerdialog.findViewById(R.id.txt_loginerror).setVisibility(View.VISIBLE);
69
}
70
}
71
catch
(Exception e)
72
{
73
e.printStackTrace();
74
}
75
finally
{
76
m_Dialog.dismiss();
77
}
78
}
79
public
void
sleep(
long
delayMillis) {
80
this
.removeMessages(
0
);
81
sendMessageDelayed(obtainMessage(
0
), delayMillis);
82
}
83
};
两个网络接口功能:
1
//调用网络接口,实现登陆指令
2
Boolean flags= UserDataServiceHelper.Login(Account, PassWord);
3
//调用网络接口,实现注册指令
4
Boolean flags= UserDataServiceHelper.Register(Account, PassWord,NiceName);
随机文章推荐
- Android的Activity组件的生命周期
- JSP的一些入门介绍
- SWT之路:选择排序演示
- 使用当前对象的引用的关键字This
- JSP开发入门
- 图解Java的数据在内存的存储
- SWT之路:SWT图像显示
- Java 素数打印
- 存取之美——HashMap原理与实践
- 方法中的参数和返回值
- 修改Android应用的样式与风格
- Java技术大牛需要学习的25个技能
- HTML (44)
- CSS (105)
- JavaScript (232)
- JQuery (53)
- Ajax (34)
- PHP (314)
- Java (39)
- C/C++ (35)
- ActionScript (21)
- PhotoShop (4)
- XML (1)
- WordPress (9)
- 数据结构 (14)
- 计算机算法 (56)
- 数据库技术 (66)
- 操作系统 (1)
- 互联网时代 (160)
- 软件项目 (27)
- 编程思想 (57)
- 搜索引擎优化 (31)
- Web设计理念 (77)
- 软件架构技术 (31)
- 信息化工具 (12)
- 广告设计 (7)
- 程序人生 (18)
- 喃喃细语 (61)
注:如需转载本文,请注明出处(原文链接),谢谢。更多精彩内容,请进入简明现代魔法首页。
- Android在对话框中实现登录与注册
- Android在对话框中实现登录与注册
- Android中使用http实现注册登录
- Android 登录对话框 实现
- Android 登录对话框 实现
- Android实现登录对话框
- Android 登录对话框 实现
- android 注册、登录实现
- android 注册、登录实现
- 使用ucenter在第三方应用中实现同步注册与登录
- 使用ucenter在第三方应用中实现同步注册与登录
- 在web中实现简单的注册,登录功能
- Servlet 实现登录与注册
- Android与J2EE交互之登录注册实现
- 【Android】 使用okhttp实现注册登录功能(与服务器端)
- jQuery实现弹出窗口中切换登录与注册表单
- rails中注册与登录的实现步骤
- android 注册、登录实现程序
- WM_PAINT介绍 及 OnPaint()函数的作用原理
- 通过ApplicationContextAware获取spring容器中的bean
- C/C++常见笔试面试题-02(网络篇)
- Android电池架构分析
- 栈的链式存储
- Android在对话框中实现登录与注册
- Ubuntu常用软件源小结
- 用C#创建Windows服务(Windows Services)
- GDB使用
- 很全的SQL注入语句,有SQL漏洞的都可以拿下
- Head Fisrt Android Development读书笔记(6)Lists and Adapters
- 数据挖掘与OLAP
- VS2010无法显示资源视图和类视图的解决办法
- linux 根据 access.log 生成压力测试脚本 监控Mysql JAVA 系统状态