关闭警报对话框按钮单击

时间:2018-04-03 11:29:12

标签: java android firebase alertdialog

在此alertdialog中,我让用户输入他们的详细信息,然后将其存储在Firebase中。当用户点击它将关闭警报对话框的mBookingbtn时,我怎么能这样做呢?

我基本上希望将数据保存到数据库时自动运行否定关闭按钮的功能

以下是代码:

final AlertDialog.Builder mBuilder = new AlertDialog.Builder(ProfileActivity.this);
                    View mView = getLayoutInflater().inflate(R.layout.welcomemessage, null);
                    final EditText mPlayer1 = (EditText) mView.findViewById(R.id.namepopup);
                    final EditText mPlayer2 = (EditText) mView.findViewById(R.id.handicappopup);
                    final EditText mPlayer3 = (EditText) mView.findViewById(R.id.agepopup);
                    final EditText mPlayer4 = (EditText) mView.findViewById(R.id.genderpopup);
                    final Button mBookingbtn = (Button) mView.findViewById(R.id.savepopup);
                    mBookingbtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String player1 = mPlayer1.getText().toString().trim();
                            String player2 = mPlayer2.getText().toString().trim();
                            String player4 = mPlayer4.getText().toString().trim();
                            String player3 = mPlayer3.getText().toString().trim();
                            if (player1.isEmpty()) {
                                mPlayer1.setError("Please enter player 1");
                                mPlayer1.requestFocus();
                                return;
                            }
                            if (player2.isEmpty()) {
                                mPlayer2.setError("Please enter player 2");
                                mPlayer2.requestFocus();
                                return;
                            }
                            if (player3.isEmpty()) {
                                mPlayer3.setError("Please enter player 2");
                                mPlayer3.requestFocus();
                                return;
                            }
                            if (player4.isEmpty()) {
                                mPlayer4.setError("Please enter player 2");
                                mPlayer4.requestFocus();
                                return;
                            }
                            String playerone = mPlayer1.getText().toString();
                            String playertwo = mPlayer2.getText().toString();
                            String playerthree = mPlayer3.getText().toString();
                            String playerfour = mPlayer4.getText().toString();
                            DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child(userid);
                            Map newPost = new HashMap();
                            newPost.put("name", playerone);
                            newPost.put("handicap", playertwo);
                            newPost.put("age", playerthree);
                            newPost.put("gender", playerfour);
                            current_user_db.setValue(newPost);
                                Toast.makeText(ProfileActivity.this, "Details Saved", Toast.LENGTH_SHORT).show();
                        }


                    });
                    mBuilder.setNeutralButton("Close ", new DialogInterface.OnClickListener() { // define the 'Cancel' button
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    mBuilder.setView(mView);
                    AlertDialog dialog = mBuilder.create();
                    dialog.show();
                }

                }

2 个答案:

答案 0 :(得分:2)

dialog.dismiss();

上面的行是用于解除对话框。 将其添加到mBookingButton单击块的代码中。

答案 1 :(得分:1)

当你点击mBookingbtn结束代码放置对话框时,忽略。 创建对话框对象作为类的本地对象。

  AlertDialog dialog;

然后在mBookingbtn上单击代码下面的代码结尾..

       if (dialog!=null){
        dialog.dismiss();
    }