【问题标题】:PopUp do something if the condition is met如果满足条件,PopUp 会执行某些操作
【发布时间】:2018-06-01 01:48:48
【问题描述】:

所以我创建了一个自定义对话框,用于询问信息(如反馈。 我希望应用程序检查所有 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,你能帮我吗?

【问题讨论】:

    标签: java android popup conditional-statements


    【解决方案1】:

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

    if(counter > 1)
    

    但您的计数器永远不会大于 1。将其更改为 if(counter > 0)if(count >= 1)

    同时使用editText.getText().toString() 来获取正确的editText 条目!

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

    【讨论】:

      【解决方案2】:

      我同意 路易斯·费尔南多·斯克里普卡鲁。 我建议你尝试一件事 editTextEmailValue.getText().toString().equals("") 而不是 editTextEmailValue.equals("")

      【讨论】:

        【解决方案3】:

        您可以删除您的 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
        }
        }
        });
        

        【讨论】:

          【解决方案4】:

          你检查

          if (check() > 1)
          

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

          所以,把它改成

          if (check() > 0)
          

          if (check() >= 1)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多