The AlertDialog class creates a dialog box, and the dialog size is set

Because the constructor of the AlertDialog class is declared protected. Therefore, you cannot create an object of the AlertDialog class through new, you need to use the Builder class, which is an embedded class defined in the AlertDialog class. First create an object instance of the AlertDialog.Builder class, and then display the dialog box through the show method of the AlertDialog.Builder class; or return the AlertDialog object through the create method, and then display the dialog box through the show method of the AlertDialog class.

 

There are many types of dialogs created by AlertDialog. First, I recorded a few I used yesterday. I have not used the progress bar and circular loading.

1. Dialog with buttons:

new AlertDialog.Builder(this).setTitle("title").setPositiveButton(...).setNegativeButton(...).setNeutralButton(...)

 

2. Simple list dialog: A simple list dialog can be created through the setItems(...) method of the AlertDialog.Builder class. In fact, this type of dialog is equivalent to placing the ListView component on the dialog, and then adding some simple text to the ListView. 


3. Single-choice list dialog: Created by setSingleChoiceItems(...) of the AlertDialog.Builder class. Currently supports 4 kinds of data sources (array resource, data set, string array, ListAdapter) 

4. Multi-choice list dialog: created by setMultiChoiceItems(...) of AlertDialog.Builder class. Currently supports 3 data sources (array resource, dataset, string array) 

 

AlertDialog dialog = new AlertDialog.Builder(mContext, R.style.dialog)
                .setTitle("Single Choice List Dialog")
                .setSingleChoiceItems(R.array.test, 0, onClickListener).create();
        dialog.show();

        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = 850;
        params.height = 545;
        dialog.getWindow().setAttributes(params); 

R.array.test   is an array of strings

DialogInterface.OnClickListener onClickListener will receive which list item is clicked.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327104273&siteId=291194637