0

我正在制作一个带有编辑文本的警报对话框。我希望它一直在屏幕上,直到用户在其中输入他的电子邮件。我怎样才能做到这一点??我的代码如下:

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 直到我需要它。提前谢谢...

4

4 回答 4

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();
于 2012-07-04T10:42:37.080 回答
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 消息。

于 2012-07-04T10:44:00.920 回答
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();
    }
});
于 2012-07-04T10:44:13.260 回答
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();
        }
于 2012-07-04T10:47:07.923 回答