如果符合条件,PopUp会做一些事情

时间:2017-12-18 17:07:42

标签: java android popup conditional-statements

所以我做了一个自定义对话框,要求提供信息(比如反馈)。 我希望应用程序检查所有EditTexts是否有值, 这是我用于打开反馈对话框的java

private void dialog() {
    myDialog.setContentView(R.layout.activity_pop_up);
    editTextEmailValue = (EditText) myDialog.findViewById(R.id.editTextEmail);
    editTextMessageValue = (EditText) myDialog.findViewById(R.id.editTextMessage);
    editTextNumeValue = (EditText) myDialog.findViewById(R.id.editTextNume);
    Button btnSend = (Button) myDialog.findViewById(R.id.sendBtn);
    Button btnClose = (Button) myDialog.findViewById(R.id.close);

    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
         //This is what I've tried (the check method is bewlow)
            if (check() > 1) {
                editTextNumeValue.setError("Obligatoriu!");
                editTextMessageValue.setError("Obligatoriu!");
                editTextEmailValue.setError("Obligatoriu!");
            } else if (check() == 0) {
                sendMail();
            }
        }
    });
    btnClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myDialog.dismiss();
        }
    });
    myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    myDialog.show();
}

这是我的检查方法:

 private int check() {
    int counter = 0;
    if (editTextEmailValue.equals("")) {
        editTextEmailValue.setError("Obligatoriu!");
        counter++;
    } else if (editTextMessageValue.equals("")) {
        counter++;
        editTextMessageValue.setError("Obligatoriu!");
    } else if (editTextNumeValue.equals("")) {
        editTextNumeValue.setError("Obligatoriu!");
        counter++;
    }
    return (counter);
}

当我按下发送按钮(用于电子邮件)时,它进入检查方法并以0 1 2 3(我只有3个textView)结束,但当它检查btnSend.onClickListener是否符合条件时仍然发送邮件,所以我可以从检查方法说,计数器一直有值0所以你能帮我吗?

4 个答案:

答案 0 :(得分:0)

你上面的逻辑错了。你正在检查

if(counter > 1)

但你的计数器永远不会大于1.将它改为 if(counter > 0)if(count >= 1)

还可以使用editText.getText().toString()获取正确的editText条目!

并使用.isEmpty()代替.equals("")

答案 1 :(得分:0)

我同意 Luis Fernando Scripcaru 。 我建议你尝试一下 editTextEmailValue.getText()。toString()。equals(“”)而不是 editTextEmailValue.equals(“”)

答案 2 :(得分:0)

你可以删除你的check()方法并试试这样它可能有效:)

int Counter = 0;

btnSend.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
     //This is what I've tried (the check method is bewlow)
if(TextUtils.isEmpty(editTextEmailValue)){
    editTextEmailValue.setError("Error");
    Counter = 1;
    }
if(TextUtils.isEmpty(editTextMessageValue)){
    editTextMessageValue.setError("Error");
    Counter = 2;
}
if(TextUtils.isEmpty(editTextNumeValue)){
    editTextNumeValue.setError("Error");
    Counter= 3;
}
if(Counter > 1){
   sendMail();
   //you can add other operations for 123 numbers too
}
}
});

答案 3 :(得分:0)

你检查

if (check() > 1)

永远不会是真的。因为check()具有IF ELSE,所以只执行一个增量并且返回值永远不会超过1,我们的检查的最大值是1.

所以,将其更改为

if (check() > 0)

if (check() >= 1)