Simple and easy to use custom Dialog (1)

Keywords: Android REST

First, a brief introduction.
In Android development, we often need to pop up some dialog boxes on the Android interface, such as asking users or letting them choose. These functions are called Android Dialog dialog boxes. Now I will introduce several ways to use Android Dialog dialog boxes, hoping to help you.
Dialog
Dialog, a dialog box, is a small window that does not fill the entire screen. It is usually displayed in modal mode, requiring users to take action to continue the rest of the operation.
Android provides rich dialog support. It provides dialog boxes commonly used in the following 4:
Alert Dialog: Warning dialog box, using one of the most extensive and versatile dialog boxes.
ProgressDialog: The progress bar dialog box is simply encapsulated.
Date Picker Dialog: Date dialog box.
TimePickerDialog: Time dialog box.
All dialogs are directly or indirectly inherited from the Dialog class, while AlterDialog is directly inherited from the Dialog class, and several other classes are inherited from the AlterDialog class.

AlertDialog
AlertDialog inherits from the Dialog class. For Android's built-in AlertDialog, it can contain a title, a content message, or a selection list with up to three buttons. Creating an AlterDialog is recommended using an internal class, AlterDialog.Builder. Using the Builder object, you can set various attributes of AlterDialog. Finally, you can get the AlterDialog object through Builder.create(). If you only need to display the AlterDialog, you can usually use the Builder.show() method directly, which will return an AlterDialog object and display it.
If you just need to prompt a piece of information to the user, you can directly use some of the properties of AlterDialog to set the prompt information. The methods involved are:
AlterDialog create(): Create an AlterDialog based on the properties set.
AlterDialog show(): Create an AlterDialog based on the properties set and display it on the screen.
AlterDialog.Builder setTitle(): Set the title.
AlterDialog.Builder setIcon(): Set the title icon.
AlterDialog.Builder setMessage(): Sets the content of the title.
AlterDialog.Builder setCancelable(): Set whether or not the mode is set to false, indicating the mode, requiring the user to take action to continue the rest of the operation.
Tips: AlterDialog.Builder's many methods of setting properties return this AlterDialog.Builder object, so you can write code in a chain way, which is more convenient.
When a dialog box calls the show() method, it is displayed on the screen. If you need to eliminate it, you can use two methods declared by the DialogInterface interface, cancel() and dismiss(), to make or eliminate the dialog box. These two methods are the same, but dismiss() is recommended. Dialog and Alter Dialog both implement the Dialog Interface interface, so they can be used to eliminate dialogs as long as they are dialog boxes.
2. Several commonly used dialog s
1) Simple dialogue

Dialog dialog = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.left_slider_alertdialog_title)
                // Create title
                .setMessage(R.string.left_slider_alertdialog_message)
                // Represents the content in the dialog box
                .setPositiveButton(R.string.left_slider_alertdialog_ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                UserInfoDBHelper.logout2();
                                refreshUserInfo();
                            }
                        })
                .setNegativeButton(R.string.left_slider_alertdialog_cancel,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create();
        dialog.show();
    }
(http://img.blog.csdn.net/20170301094527677?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzcyMzcyNDU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)   
2)
    AlterDialog Button

AlterDialog has three built-in buttons, which can be set directly using setXxxButton() method. For General dialog boxes, three buttons are basically enough. Here are the signatures of these three methods:
AlterDialog. Builder setPositiveButton (CharSquence text, DialogInterFace. OnClickListener): An active button, commonly used for "OK" or "Continue" operations.
AlterDialog. Builder setNegative Button (CharSquence text, DialogInterFace. OnClickListener): A negative button commonly used for "Cancel" operations.
AlterDialog. Builder setNeutral Button (CharSquence text, DialogInterFace. OnClickListener): A relatively neutral button, commonly used for "ignore" and "remind me later" operations.
The DialogInterface interface described above also provides a series of event responses. All three buttons need to pass a DialogInterFace.OnClickListener interface object to trigger the click event. In this interface, we need to implement an onClick (DialogInterface Diaog, int which is the dialog object interface of the current triggering event, which can be directly used. Mandatory conversion to AlterDialog for operation; while is the button-clicking identifier and is a shaping data. For these three buttons, each button uses different int-type data to identify: Positive (-1), Negative(-2), Neutral(-3).
In addition to DialogInterFace.OnClickListener events, which are specifically implemented for button clicks, DialogInterface also provides some other events for Dialog objects to respond to. These events are only responses to the states of the Dialog declaration cycle. As soon as you see, they will not be explained in detail. Here are the descriptions of these events:
InterfaDialogInterface. OnCancelListener: Triggered when the dialog box calls the cancel() method.
InterfaDialogInterface. OnDismissListener: Triggered when the dialog box calls the dismiss() method.
InterfaDialogInterface. OnShowListener: Triggered when the dialog box calls the show() method.
interface DialogInterface.OnMultiChoiceListener: Triggered when the dialog box uses a multiselect list and is selected.

       AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this);
                 builder.setTitle("Tips");
                 builder.setMessage("This is a multi-button ordinary dialog box!");
                builder.setIcon(R.drawable.ic_launcher);
                builder.setPositiveButton("Sure?", new OnClickListener() {

                     @Override
                     public void onClick(DialogInterface dialog, int which) {                        
                         Toast.makeText(MainActivity.this, "Make sure it's clicked",
                                Toast.LENGTH_SHORT).show();
                         dialog.dismiss();
                     }
                 });
                 builder.setNegativeButton("negative", new OnClickListener() {

                     @Override
                    public void onClick(DialogInterface dialog, int which) {
                         // TODO Auto-generated method stub
                         Toast.makeText(MainActivity.this, "Negative is clicked",
                                Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                   }
                 });
                 builder.setNeutralButton("ignore", new OnClickListener() {

                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                         // TODO Auto-generated method stub
                         Toast.makeText(MainActivity.this, "Ignore being clicked",
                                Toast.LENGTH_SHORT).show();
                         dialog.cancel();
                     }
                 });
                 builder.show();
![Here's a picture description.](http://img.blog.csdn.net/20170301102756338?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzcyMzcyNDU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

3) List form of AlterDialog
In addition to presenting some hints, AlterDialog can also display a list format, which needs to be set using the Builder.setItems(CharSequence[] items,DialogInterface.OnClickListener listener) method. It needs to pass an array of CharSequenece types to bind the list data. It also needs to pass a DialogInterface.OnClickListener interface. To respond to a click on a list item, and the which parameter of the onClick method in this interface is the subscript in the items that are currently clicking on the trigger item.
  

btnListView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
       AlertDialog.Builder builder = new AlertDialog.Builder(
               MainActivity.this);
        builder.setTitle("Please choose the city");
        //items uses the global finalCharSequenece array declaration
        builder.setItems(items, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                String select_item = items[which].toString();
                Toast.makeText(MainActivity.this,
                        "Chose--->" + select_item, Toast.LENGTH_SHORT)
                        .show();
            }
        });
        builder.show();
    }
});       

4 single check box

new AlertDialog.Builder(this).setTitle("check box").setMultiChoiceItems(
       new String[] { "Item1", "Item2" }, null, null)
       .setPositiveButton("Sure?", null)
       .setNegativeButton("cancel", null).show();

5) dialog for filling in information
Information content is a simple View type

new AlertDialog.Builder(this).setTitle("Please input").setIcon(
       android.R.drawable.ic_dialog_info).setView(
       new EditText(this)).setPositiveButton("Sure?", null)
       .setNegativeButton("cancel", null).show();

6) Multi-check box

new AlertDialog.Builder(this).setTitle("Radio").setIcon(
       android.R.drawable.ic_dialog_info).setSingleChoiceItems(
       new String[] { "Item1", "Item2" }, 0,
       new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
         dialog.dismiss();
        }
       }).setNegativeButton("cancel", null).show();

Custom view will be
A simple and easy-to-use custom Dialog (2) is described in detail...

Posted by plasmahba on Mon, 08 Apr 2019 18:15:30 -0700