android alertDialog 点击按钮后保留dialog
2012-11-01 15:31
337 查看
登录框实例:
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
builder.setTitle(Constants.tipTitle);
builder.setView(loginView);
builder.setPositiveButton("登录", null);
builder.setNegativeButton("取消", null);
final android.app.AlertDialog alert = builder.create();
alert.show();
Button theButton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
TextView untv = (TextView)loginView.findViewById(R.id.userName);
if(untv.getText().length()==0){
Toast.makeText(ctx, "请输入用户名", Constants.Toast.SHORT).show();
return;
}
TextView password = (TextView)loginView.findViewById(R.id.password);
if(password.getText().length()==0){
Toast.makeText(ctx, "请输入密码", Constants.Toast.SHORT).show();
return;
}
alert.dismiss();
}
});
简要分析:
原先由builder.setPositiveButton("登录", listener); builder.setNegativeButton("取消", listener); 实现,点击后dialog会自动消失。
但在验证信息时,需要验证dialog中的表单字段,若验证不通过,就应该提示用户,同时保留dialog供用户纠正。
优化后的实现则,设置为null,在dialog呈现后,获取其中的按钮,就像普通按钮一样进行事件的绑定。
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ctx);
builder.setTitle(Constants.tipTitle);
builder.setView(loginView);
builder.setPositiveButton("登录", null);
builder.setNegativeButton("取消", null);
final android.app.AlertDialog alert = builder.create();
alert.show();
Button theButton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
TextView untv = (TextView)loginView.findViewById(R.id.userName);
if(untv.getText().length()==0){
Toast.makeText(ctx, "请输入用户名", Constants.Toast.SHORT).show();
return;
}
TextView password = (TextView)loginView.findViewById(R.id.password);
if(password.getText().length()==0){
Toast.makeText(ctx, "请输入密码", Constants.Toast.SHORT).show();
return;
}
alert.dismiss();
}
});
简要分析:
原先由builder.setPositiveButton("登录", listener); builder.setNegativeButton("取消", listener); 实现,点击后dialog会自动消失。
但在验证信息时,需要验证dialog中的表单字段,若验证不通过,就应该提示用户,同时保留dialog供用户纠正。
优化后的实现则,设置为null,在dialog呈现后,获取其中的按钮,就像普通按钮一样进行事件的绑定。
相关文章推荐
- Android开发-UI控件:让AlertDialog在按钮被点击后不消失
- Android下关于AlertDialog点击 确定或者取消按钮 对话框自动消失的 解决办法
- android alertdialog的按钮点击后不消失
- Android的AlertDialog点击确认键dialog不消失
- 【Android】AlertDialog点击按钮后不自动关闭对话框
- Android:在AlertDialog点击按钮隐藏对话框
- android点击AlertDialog上的按钮后让其不消失
- Android Dialog点击按钮不关闭,控制窗口的显示和关闭
- Android AlertDialog解决点击按钮立即消失的问题
- Android设置AlertDialog点击按钮对话框不关闭(转)
- Android alertdialog的自定义添加按钮和添加点击
- Android Alert Dialog解决点击按钮对话框不关闭/的问题
- Android 自定义的Dialog,点击确定按钮,让其根据条件判断消失与否
- android开发之AlertDialog点击按钮之后不消失 分类: android 学习笔记 2015-07-15 18:07 89人阅读 评论(0) 收藏
- Android自定义样式(风格)的dialog对话框(点击其中的按钮跳转到下一个页面)
- android开发之AlertDialog点击按钮之后不消失
- Android开发,禁止AlertDialog点击确定或者取消都自动关闭dialog的情况
- Android alertdialog的按钮点击后不消失
- 让AlertDialog在按钮被点击后不消失【android搞死你】
- Android实现点击AlertDialog上按钮时不关闭对话框的方法