How to implement a custom AlertDialog View

前端 未结 11 897
悲&欢浪女
悲&欢浪女 2020-11-22 13:18

In the Android docs on AlertDialog, it gives the following instruction and example for setting a custom view in an AlertDialog:

If you want to display a
11条回答
  •  难免孤独
    2020-11-22 13:39

    The easiest way to do this is by using android.support.v7.app.AlertDialog instead of android.app.AlertDialog where public AlertDialog.Builder setView (int layoutResId) can be used below API 21.

    new AlertDialog.Builder(getActivity())
        .setTitle(title)
        .setView(R.layout.dialog_basic)
        .setPositiveButton(android.R.string.ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //Do something
                }
            }
        )
        .setNegativeButton(android.R.string.cancel,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //Do something
                }
            }
        )
        .create();
    

提交回复
热议问题