In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.