内容简介:我创建了简单的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) 版本更新,支持自定义按钮图片,新标签,视频文件预览
- 按钮穿透点击实现方式
- 如何更好的控制按钮样式
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Art of Computer Programming, Volume 4, Fascicle 3
Donald E. Knuth / Addison-Wesley Professional / 2005-08-05 / USD 19.99
Finally, after a wait of more than thirty-five years, the first part of Volume 4 is at last ready for publication. Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and ......一起来看看 《The Art of Computer Programming, Volume 4, Fascicle 3》 这本书的介绍吧!