Simplest yes/no dialog fragment

后端 未结 4 896
無奈伤痛
無奈伤痛 2020-12-12 16:55

I\'d like to make a dialog fragment that asks \"Are you sure?\" with a \"yes/no\" reply.

I\'ve looked at the documentation and it\'s really verbose, go

4条回答
  •  旧巷少年郎
    2020-12-12 17:37

    So, what's the simplest way to create and display a really basic Alert Dialog? Bonus points if it's using the support library.

    Simply create a DialogFragment(SDK or support library) and override its onCreateDialog method to return an AlertDialog with the desired text and buttons set on it:

    public static class SimpleDialog extends DialogFragment {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.Builder(getActivity())
                    .setMessage("Are you sure?")
                    .setPositiveButton("Ok", null)
                    .setNegativeButton("No way", null)
                    .create();
        }
    
    }
    

    To do something when the user uses one of the buttons you'll have to provide an instance of a DialogInterface.OnClickListener instead of the null references from my code.

提交回复
热议问题