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

问题描述 投票:0回答:4

所以我创建了一个自定义对话框,要求提供信息(比如反馈。我希望应用程序检查所有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如果条件满足它仍然发送邮件,所以从那我可以说从check方法来看,计数器一直有0值,所以你能帮帮我吗?

java android popup condition
4个回答
0
投票

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

if(counter > 1)

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

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

并使用.isEmpty()而不是.equals("")


0
投票

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


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
}
}
});

0
投票

你检查

if (check() > 1)

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

所以,改成这个

if (check() > 0)

要么

if (check() >= 1)
© www.soinside.com 2019 - 2024. All rights reserved.