内容简介:我创建了简单的AlertDialog,带有正负按钮.正按钮已注册DialogInterface.OnClickListener,在那里我获得EditText值.我必须验证它(例如,如果它不为null),如果值不正确,不允许关闭此对话框.点击后如何防止关闭对话框验证?http://stackoverflow.com/questions/11363209/alertdialog-with-positive-button-and-validating-custom-edittext
我创建了简单的AlertDialog,带有正负按钮.正按钮已注册DialogInterface.OnClickListener,在那里我获得EditText值.我必须验证它(例如,如果它不为null),如果值不正确,不允许关闭此对话框.点击后如何防止关闭对话框验证?
对话创建:
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
builder.setCancelable(false)
.setMessage("Please Enter data")
.setView(edtLayout) //<-- layout containing EditText
.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//All of the fun happens inside the CustomListener now.
//I had to move it to enable data validation.
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
Button theButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(alertDialog));
CustomListener:
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
// put your code here
String mValue = mEdtText.getText().toString();
if(validate(mValue)){
dialog.dismiss();
}else{
Toast.makeText(YourActivity.this, "Invalid data", Toast.LENGTH_SHORT).show();
}
}
}
http://stackoverflow.com/questions/11363209/alertdialog-with-positive-button-and-validating-custom-edittext
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- flex自定义按钮皮肤示例附图
- 自定义按钮 图片标题位置随意放置
- 游戏制作之路(31)创建自定义的按钮
- QTTabBar 1.5.3-beta(2020) 版本更新,支持自定义按钮图片,新标签,视频文件预览
- 按钮穿透点击实现方式
- 如何更好的控制按钮样式
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
算法:C语言实现
塞奇威克 / 霍红卫 / 机械工业出版社 / 2009-10 / 79.00元
《算法:C语言实现(第1-4部分)基础知识、数据结构、排序及搜索(原书第3版)》细腻讲解计算机算法的C语言实现。全书分为四部分,共16章。包括基本算法分析原理,基本数据结构、抽象数据结构、递归和树等数据结构知识,选择排序、插入排序、冒泡排序、希尔排序、快速排序方法、归并和归并排序方法、优先队列与堆排序方法、基数排序方法以及特殊用途的排序方法,并比较了各种排序方法的性能特征,在进一步讲解符号表、树等......一起来看看 《算法:C语言实现》 这本书的介绍吧!