Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use AlertDialog in Android

Shulou Source: shulou.com Published: 2022-06-02 07:07:39 10月27日 Update

This article introduces how to use AlertDialog in Android, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Layout file Layout

Then add four Button buttons to the layout file of layout

The effect is as follows:

Code file

We define two arrays in the code file

Dialog box selection results for dealing with multiple selections

The basic method of creating AlertDialog:

To construct content such as titles, icons, and buttons through an AlertDialog.Builder.

Create an object for the constructor AlertDialog.Builder

The title, information and icon of the dialog box are constructed by calling setTitle, setMessage and other methods through the object of the constructor.

Set positive, negative and neutral buttons as needed

Call the create method to create an object of AlertDialog

The object of AlertDialog calls the show method to make the dialog box appear on the interface.

AlertDialog.Builder itself has a show method that displays dialog boxes, so steps 4 and 5 above can be simplified to one step.

Basic implementation code:

/ / Dialog box with list

Button btn2 = (Button) findViewById (R.id.button2)

Btn2.setOnClickListener (new View.OnClickListener () {

@ Override

Public void onClick (View view) {

Final String [] items = new String [] {"running", "swimming", "badminton", "table tennis", "tennis"}

/ / create a constructor

AlertDialog.Builder builder = new AlertDialog.Builder (MainActivity.this)

/ / set the display icon of AlertDialog

Builder.setIcon (R.mipmap.ic_launcher)

/ / set the display title of AlertDialog

Builder.setTitle ("Please choose your favorite sport:")

/ / add a list item to AlertDialog. The second parameter is to write the click event directly.

Builder.setItems (items, new DialogInterface.OnClickListener () {

@ Override

Public void onClick (DialogInterface dialogInterface, int I) {

Toast.makeText (MainActivity.this, "you chose" + items [I], Toast.LENGTH_SHORT) .show ()

}

});

/ / create and display the constructor of our entire AlertDialog

Builder.create () .show ()

}

});

Dialog box AlertDialog

To add setMessage to the displayed content, then setbutton is to set the added button and write the corresponding event

Set the list AlertDialog for radio selection

Need to use setSingleChoiceItems

Set the multi-selected list AlertDialog

Need to use setMultiChoiceItems

The array of checkitems we defined earlier is used to record whether the selection is selected or not.

Then iterate through the selected state when determining the button to find the corresponding item of our choice.

On how to use AlertDialog in Android to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Buttons dialogs dialogs content constructors selections objects files methods code icons titles events layouts arrays times more status projects help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Redmi Linux vpn