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

后端 未结 18 1966
无人及你
无人及你 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

    For pre API 8 i solved the problem using a boolean flag, a dismiss listener and calling dialog.show again if in case the content of the editText wasn´t correct. Like this:

    case ADD_CLIENT:
            LayoutInflater factoryClient = LayoutInflater.from(this);
            final View EntryViewClient = factoryClient.inflate(
                    R.layout.alert_dialog_add_client, null);
    
            EditText ClientText = (EditText) EntryViewClient
                    .findViewById(R.id.client_edit);
    
            AlertDialog.Builder builderClient = new AlertDialog.Builder(this);
            builderClient
                    .setTitle(R.string.alert_dialog_client)
                    .setCancelable(false)
                    .setView(EntryViewClient)
                    .setPositiveButton("Save",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int whichButton) {
                                    EditText newClient = (EditText) EntryViewClient
                                            .findViewById(R.id.client_edit);
                                    String newClientString = newClient
                                            .getText().toString();
                                    if (checkForEmptyFields(newClientString)) {
                                        //If field is empty show toast and set error flag to true;
                                        Toast.makeText(getApplicationContext(),
                                                "Fields cant be empty",
                                                Toast.LENGTH_SHORT).show();
                                        add_client_error = true;
                                    } else {
                                        //Here save the info and set the error flag to false
                                        add_client_error = false;
                                    }
                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    add_client_error = false;
                                    dialog.cancel();
                                }
                            });
            final AlertDialog alertClient = builderClient.create();
            alertClient.show();
    
            alertClient
                    .setOnDismissListener(new DialogInterface.OnDismissListener() {
    
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            //If the error flag was set to true then show the dialog again
                            if (add_client_error == true) {
                                alertClient.show();
                            } else {
                                return;
                            }
    
                        }
                    });
            return true;
    

提交回复
热议问题