Custom Layout for DialogFragment OnCreateView vs. OnCreateDialog

后端 未结 6 1614
庸人自扰
庸人自扰 2020-12-01 00:31

I\'m trying to create a DialogFragment using my own Layout.

I\'ve seen a couple different approaches. Sometimes the layout is set in OnCreateDialog like this: (I\'

6条回答
  •  被撕碎了的回忆
    2020-12-01 01:08

    Here's an example of using findViewById in a Dialog Fragment

    public class NotesDialog extends DialogFragment {
    
            private ListView mNotes;
           private RelativeLayout addNote;
    
            public NotesDialog() {
                // Empty constructor required for DialogFragment
            }
    
    
    
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
    
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    
                View view = getActivity().getLayoutInflater().inflate(R.layout.note_dialog, null);
                mNotes = (ListView) view.findViewById(R.id.listViewNotes);
                addNote = (RelativeLayout) view.findViewById(R.id.notesAdd);
    
                addNote.setOnClickListener(new View.OnClickListener(){
                     @Override
                     public void onClick(View v){
    
    
                         getDialog().dismiss();
    
                         showNoteDialog();
                     }
                 });
    
                builder.setView(view);
    
                builder.setTitle(bandString);
    
    
                builder.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                              getDialog().dismiss();
                            }
                        }
                    );
    
    
               return  builder.create();
    
    
        }
    

提交回复
热议问题