How to prevent a dialog from closing when a button is clicked

后端 未结 18 2165
无人及你
无人及你 2020-11-21 23:59

I have a dialog with EditText for input. When I click the \"yes\" button on dialog, it will validate the input and then close the dialog. However, if the input

18条回答
  •  失恋的感觉
    2020-11-22 00:42

    you can add builder.show(); after validation message before return;

    like this

        public void login()
    {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(R.layout.login_layout);
        builder.setTitle("Login");
    
    
    
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });// put the negative button before the positive button, so it will appear
    
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                Dialog d = (Dialog) dialog;
                final EditText etUserName = (EditText) d.findViewById(R.id.etLoginName);
                final EditText etPassword = (EditText) d.findViewById(R.id.etLoginPassword);
                String userName = etUserName.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
    
                if (userName.isEmpty() || password.isEmpty())
                {
    
                    Toast.makeText(getApplicationContext(),
                            "Please Fill all fields", Toast.LENGTH_SHORT).show();
                    builder.show();// here after validation message before retrun
                                   //  it will reopen the dialog
                                  // till the user enter the right condition
                    return;
                }
    
                user = Manager.get(getApplicationContext()).getUserByName(userName);
    
                if (user == null)
                {
                    Toast.makeText(getApplicationContext(),
                            "Error ethier username or password are wrong", Toast.LENGTH_SHORT).show();
                    builder.show();
                    return;
                }
                if (password.equals(user.getPassword()))
                {
                    etPassword.setText("");
                    etUserName.setText("");
                    setLogged(1);
                    setLoggedId(user.getUserId());
                    Toast.makeText(getApplicationContext(),
                            "Successfully logged in", Toast.LENGTH_SHORT).show();
                   dialog.dismiss();// if every thing is ok then dismiss the dialog
                }
                else
                {
                    Toast.makeText(getApplicationContext(),
                            "Error ethier username or password are wrong", Toast.LENGTH_SHORT).show();
                    builder.show();
                    return;
                }
    
            }
        });
    
        builder.show();
    
    }
    

提交回复
热议问题