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

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

    Use a custom layout for your DialogFragment and add a LinearLayout under your content which can be styled as borderless to match Google Material Design. Then find the newly created buttons and override their OnClickListener.

    Example:

    public class AddTopicFragment extends DialogFragment {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            // Get the layout inflater
            LayoutInflater inflater = getActivity().getLayoutInflater();
            final View dialogView = inflater.inflate(R.layout.dialog_add_topic, null);
    
            Button saveTopicDialogButton = (Button) dialogView.findViewById(R.id.saveTopicDialogButton);
            Button cancelSaveTopicDialogButton = (Button) dialogView.findViewById(R.id.cancelSaveTopicDialogButton);
    
            final AppCompatEditText addTopicNameET = (AppCompatEditText) dialogView.findViewById(R.id.addTopicNameET);
            final AppCompatEditText addTopicCreatedByET = (AppCompatEditText) dialogView.findViewById(R.id.addTopicCreatedByET);
    
            saveTopicDialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // validate inputs
                    if(addTopicNameET.getText().toString().trim().isEmpty()){
                        addTopicNameET.setError("Topic name can't be empty");
                        addTopicNameET.requestFocus();
                    }else if(addTopicCreatedByET.getText().toString().trim().isEmpty()){
                        addTopicCreatedByET.setError("Topic created by can't be empty");
                        addTopicCreatedByET.requestFocus();
                    }else {
                        // save topic to database
                        Topic topic = new Topic();
                        topic.name = addTopicNameET.getText().toString().trim();
                        topic.createdBy = addTopicCreatedByET.getText().toString().trim();
                        topic.createdDate = new Date().getTime();
                        topic.save();
                        AddTopicFragment.this.dismiss();
                    }
                }
            });
    
            cancelSaveTopicDialogButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AddTopicFragment.this.dismiss();
                }
            });
    
            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(dialogView)
                   .setMessage(getString(R.string.add_topic_message));
    
            return builder.create();
        }
    
    }
    

    dialog_add_topic.xml:

    
    
    
        
    
            
    
        
    
        
    
            
    
        
    
        
            

    This is the final result.

提交回复
热议问题