我正在制作一个包含编辑文字的提醒对话框。我希望它会保留在屏幕上,直到用户输入他的电子邮件。我怎样才能做到这一点??我的代码如下:
final AlertDialog.Builder alert= new AlertDialog.Builder(this);
alert.setMessage("Enter Email:");
final EditText userid = new EditText(this);
alert.setView(userid);
userid.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (userid.getText().toString().length()>0){
userId = userid.getText().toString();
result();
}else{
Toast.makeText(getApplicationContext(), "Enter your email for future reference.", Toast.LENGTH_SHORT).show();
}
}
});
alert.show();
单击对话框关闭的任何按钮。请告诉我如何在屏幕上保留dailog,直到我需要它为止。提前等待......
答案 0 :(得分:1)
试试这个,
final AlertDialog.Builder alert= new AlertDialog.Builder(this);
alert.setMessage("Enter Email:");
final EditText userid = new EditText(this);
alert.setView(userid);
alert.setCancelable(false);
userid.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (userid.getText().toString().length()>0){
userId = userid.getText().toString();
result();
dialog.dismiss();
}else{
Toast.makeText(getApplicationContext(), "Enter your email for future reference.", Toast.LENGTH_SHORT).show();
}
}
});
alert.show();
答案 1 :(得分:1)
做这样的事......
boolean flagEmail = false;
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
if (validate(email.getText().toString().trim()))
{
flagEmail = true;
}
调用验证函数
public boolean validate(String emailId) {
matcher = pattern.matcher(emailId);
return matcher.matches();
}
在你的条件中,如果flagEmail = true;
然后让对话框按钮取消,否则得到一个Toast消息。
答案 2 :(得分:1)
您只需设置它就可以取消对话:
alert.setCancelable(false);
然后,如果用户在没有输入数据的情况下按下OK按钮,您可以执行以下操作:
alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
public void onDismiss( DialogInterface dialog) {
//If userid get text not greater than 0
//then reshow the alertdialog with alert.show();
}
});
答案 3 :(得分:1)
alert.setCancelable(false);
并使用TextUtils.isBlank(email)
String email = userid.getText().toString().trim();
if(!(TextUtils.isBlank(email))){//check here
}else{
Toast.makeText(getApplicationContext(), "Enter your email for future reference.",
Toast.LENGTH_SHORT).show();
}