For Android, the Common Dialog Box, single-choice dialog box, multi-choice dialog box, Icon-based dialog box, and custom Adapter and custom View dialog box are described in detail.

Source: Internet
Author: User

For Android, the Common Dialog Box, single-choice dialog box, multi-choice dialog box, Icon-based dialog box, and custom Adapter and custom View dialog box are described in detail.

The dialog box is an AlertDialog, but a simple AlertDialog, we can make a lot of things about it. Next we will summarize the usage of AlertDialog. Check whether all of your shoes use these AlertDialog features in their daily work.

OK. Let's get into our current topic.

Normal dialog box

A normal dialog box is the most commonly used dialog box. The implementation is not complicated. The implementation result is of course the simplest, as shown below:

 

AlertDialog dialog = new AlertDialog. builder (this ). setTitle ("normal dialog box "). setIcon (R. drawable. a4s ). setNegativeButton ("cancel", null ). setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// processing confirmation button click event }}). setNeutralButton ("neutral", null ). setMessage ("are you sure you want to delete it? "). Create (); dialog. show ();


 

We can set the title or icon for a dialog box. In the bottom of the dialog box, we can set three buttons: Cancel, confirm, and neutral. In general, we can add corresponding click events in the confirmation button. OK, so what is the display effect of such a dialog box? As follows:

Show Item dialog box

If we want to display several items in the dialog box, we can set items for AlertDialog as follows:

 

AlertDialog dialog = new AlertDialog. builder (this ). setTitle ("display Item dialog box "). setIcon (R. drawable. a4s ). setItems (items, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, items [which], Toast. LENGTH_SHORT ). show ();}}). create (); dialog. show ();

Here, items is an array of the String type, as follows:

 

 

Items = new String [] {"wei", "Shu", "Wu "};

Here, we do not need to set the three buttons at the bottom, and directly click the Item to trigger the corresponding event. In The onClick method, there is an int type parameter called which. This parameter indicates the number of items clicked by the user. The result is as follows:

 

After a user clicks a different item, The onClick method processes the item based on the value of which.

Radio dialog box

Similar to the dialog box that displays items, we do not need to set the bottom three buttons in the single-choice dialog box (if you have to set the buttons). The Code is as follows:

 

AlertDialog dialog = new AlertDialog. builder (this ). setTitle ("single choice dialog box "). setIcon (R. drawable. a4s ). setSingleChoiceItems (items,-1, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, items [which], Toast. LENGTH_SHORT ). show (); dialog. dismiss ();}}). create (); dialog. show ();

The method we call this time is setSingleChoiceItems. This method has three parameters. The items value is the same as the previous one. The second parameter indicates the item in the pre-selection, the third parameter is the event processing method. The event processing method is the same. The difference is that there is an additional dialog. dismiss ();, that's right. here we need to manually close the dialog. Then its display effect is as follows:

 

When we select any item, the dialog will be automatically disabled, and the corresponding click event will be processed in the onClick method.

Multiple choice dialog box

Unlike the single-choice dialog box, the multi-choice dialog box requires that we have the following buttons. Here I will set two buttons for the following. Let's look at the Code:

 

AlertDialog dialog = new AlertDialog. builder (this ). setTitle ("Multi-choice dialog box "). setIcon (R. drawable. a4s ). setNegativeButton ("cancel", null ). setPositiveButton ("OK", null ). setMultiChoiceItems (items, null, new OnMultiChoiceClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which, boolean isChecked ){}}). create (); dialog. show ();

Like setSingleChoiceItems, The setMultiChoiceItems here also has three parameters: data source, pre-selected item, and item click event. The pre-selected item is a Boolean array, indicates which items are selected at the beginning and which items are not selected. Here, the click event has an additional parameter isChecked, indicating whether the item is selected, then we can use a boolean array to record the selected items. I will not write it separately here. Shows the Display Effect of the multiple choice dialog box:

 

After selecting each item, click OK to get the corresponding data.

Custom Adapter

Some people may have noticed that Builder also has a method called setAdapter. The parameter of this method is a ListAdapter, which is the Adapter we usually create for ListView, you can set the Adapter, which means that our AlertDialog can display more content. Here I will use the simplest ArrayAdapter to create a Demo for you. The Code is as follows:

 

Adapter = new ArrayAdapter
 
  
(This, android. r. layout. simple_list_item_1, new String [] {"Zhang San", "Li Si"}); AlertDialog dialog = new AlertDialog. builder (this ). setTitle ("Custom Adapter "). setIcon (R. drawable. a4s ). setAdapter (adapter, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which ){}}). create (); dialog. show ();
 

The display effect is as follows:

 

At this time, we want to make AlertDialog show what it looks like, and the Adapter brings us great flexibility.

Custom View

Of course, the ultimate Boss is not a custom Adapter, but a custom View. Through a custom View, we can make our AlertDialog display any interface we want. The Code is as follows:

 

View view = LayoutInflater. from (this ). inflate (R. layout. activity_main, null); AlertDialog dialog = new AlertDialog. builder (this ). setTitle ("Custom View "). setIcon (R. drawable. a4s ). setView (view ). create (); dialog. show ();

The display effect is as follows:

 

By using a custom View, we can customize a variety of Dialog, such as the logged-on Dialog, or the Dialog about or description.

AlertDialog is basically used in the following ways:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.