In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to understand the common dialog box AlertDialog, which may not be understood by many people. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
In Android application, there are many dialogs: Dialog, AlertDialog, ProgressDialog, time, date and so on.
(1) the Dialog class is the base class of all dialogs, it should be noted that although the Dialog class can be displayed on the interface, it is not inherited and accustomed to the View class, but is constructed directly from java.lang.Object, similar to Activity,Dialog also has a life cycle, its life cycle is maintained by Activity. Activity is responsible for producing, saving, and replying to it, and there are callback functions for system direction calls at each stage of the life cycle.
(2) AlertDialog is a direct subclass of Dialog, and AlertDialog is also one of the most commonly used dialogs in Android systems. An AlertDialog can have two Button or three Button, and you can set title and message for one AlertDialog. You cannot generate an AlertDialog directly through the constructor of AlertDialog. Generally, AlertDialog is generated through its internal static class AlertDialog.builder.
(3) as the name implies, this Dialog, which is a subclass of AlertDialog, is responsible for displaying progress to the user.
In this chapter, we focus on AlertDialog!
The constructors of AlertDialog are all Protected, so you can't create an AlertDialog directly through new an AlertDialog.
To create an AlertDialog, you use the create () method in AlertDialog.Builder.
To create a dialog box using AlertDialog.Builder, you need to know the following methods:
SetTitle: sets the title for the dialog box
SetIcon: setting icons for dialog boxes
SetMessage: setting content for dialog box
SetView: setting custom styles for dialog boxes
SetItems: sets a list to be displayed in the dialog box, which is generally used when displaying several commands
SetMultiChoiceItems: used to set the dialog box to display a series of check boxes
SetNeutralButton: normal button
SetPositiveButton: add "Yes" button "OK" to the dialog box
SetNegativeButton: add "No" button to the dialog box to "cancel"
Create: create dialog box
Show: displaying dialog box
1. Simple dialog box
Although the Dialog class can be displayed on the interface, it is not an inherited and habitual View class, but is constructed directly from java.lang.Object.
1. Open the src/com.genwoxue.alertdialog_a/MainActivity.java file.
Then enter the following code:
Import android.app.Activity
Import android.app.AlertDialog
Import android.app.AlertDialog.Builder
Import android.os.Bundle
Import android.view.Menu
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
SetContentView (R.layout.activity_main)
/ / the constructors of AlertDialog are all Protected, so you cannot create an AlertDialog directly through new an AlertDialog.
/ / to create an AlertDialog, use the create () method in AlertDialog.Builder
Builder adInfo = new AlertDialog.Builder (this)
AdInfo.setTitle ("simple dialog box"); / / set the title
AdInfo.setMessage ("this is a beautiful legend, beautiful stones can sing …") ; / / set the content
AdInfo.create ()
AdInfo.show (); / / Open app and the dialog box pops up. Click on the dialog box outside the dialog box and it will disappear.
}
}
2. Run and display the interface:
2. AlertDialog with buttons
When we perform operations such as deletion, confirmation, etc., we often click buttons in the dialog box, and AlertDialog can display three buttons.
1. Open the src/com.genwoxue.alertdialog_bMainActivity.java file.
Then enter the following code:
Import android.app.Activity
Import android.app.AlertDialog
Import android.app.AlertDialog.Builder
Import android.content.DialogInterface
Import android.os.Bundle
Import android.view.Menu
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
SetContentView (R.layout.activity_main)
Builder dialog = new AlertDialog.Builder (this)
Dialog.setTitle ("confirm deletion?")
Dialog.setMessage ("are you sure to delete this message?")
Dialog.setIcon (R.drawable.ic_launcher)
/ / register the listening event for the OK button
Dialog.setPositiveButton ("OK", new DialogInterface.OnClickListener () {
@ Override
Public void onClick (DialogInterface arg0, int arg1) {
/ / write the corresponding code according to the actual situation
}
});
/ / register the listening event for the cancel button
Dialog.setNegativeButton ("cancel", new DialogInterface.OnClickListener () {
@ Override
Public void onClick (DialogInterface arg0, int arg1) {
/ / write the corresponding code according to the actual situation
}
});
Dialog.setNeutralButton ("View details", new DialogInterface.OnClickListener () {
@ Override
Public void onClick (DialogInterface arg0, int arg1) {
/ / write the corresponding code according to the actual situation
}
});
Dialog.create ()
Dialog.show ()
}
}
2. Run and display the interface:
3. AlertDialog dialog box with radio button and similar to ListView
The setSingleChoiceItems (CharSequence [] items, int checkedItem,final OnClickListener listener) method implements AlertDialog similar to ListView, with the first parameter being an array of data to be displayed, the second parameter specifying the default selection, and the third parameter setting listening for handling events.
1. Open the src/com.genwoxue.alertdialog_c/MainActivity.java file.
Then enter the following code:
Import android.app.Activity
Import android.app.AlertDialog
Import android.app.Dialog
Import android.content.DialogInterface
Import android.os.Bundle
Import android.view.Menu
Import android.widget.Toast
Public class MainActivity extends Activity {
/ / declare the selected variable
Private int selectedCityIndex = 0
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
SetContentView (R.layout.activity_main)
/ / define an array of cities
Final String [] arrayCity = new String [] {"Hangzhou", "New York", "Venice", "Hokkaido"}
/ / instantiate AlertDialog dialog box
Dialog alertDialog = new AlertDialog.Builder (this)
.setTitle ("what's your favorite place") / / set the title
.setIcon (R.drawable.ic_launcher) / / set icon
/ / the setup dialog box displays a radio List, specifies the default selection, and sets the listening event handling.
.setSingleChoiceItems (arrayCity, 0, new DialogInterface.OnClickListener () {
@ Override
Public void onClick (DialogInterface dialog, int which) {
/ / the index of the selected item is saved to the selected item variable
SelectedCityIndex = which
}
})
/ / add cancel button and add snooping processing
.setNegativeButton ("cancel", new DialogInterface.OnClickListener () {
@ Override
Public void onClick (DialogInterface arg0, int arg1) {
/ / method stubs automatically generated by TODO
}
})
/ / add OK button and add listening processing
.setPositiveButton ("OK", new DialogInterface.OnClickListener () {
@ Override
Public void onClick (DialogInterface arg0, int arg1) {
/ / display the selected value
Toast.makeText (getApplicationContext (), arrayCity [selectedCityIndex], Toast.LENGTH_SHORT) .show ()
}
}) .create ()
AlertDialog.show (); / / .show () must be written separately and cannot be followed by the above
}
}
2. Run and display the interface:
4. AlertDialog dialog box with check box and similar to ListView
The setMultiChoiceItems (CharSequence [] items, boolearn [] checkedItems,final OnMultiChoiceClickListener listener) method is used to implement AlertDialog similar to ListView. The first parameter is an array of data to be displayed, the second parameter specifies the default selection, and the second parameter sets the listening event.
1. Open the src/com.genwoxue.alertdialog_d/MainActivity.java file.
Then enter the following code:
Import android.app.Activity
Import android.app.AlertDialog
Import android.app.Dialog
Import android.content.DialogInterface
Import android.os.Bundle
Import android.widget.Toast
Public class MainActivity extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
SetContentView (R.layout.activity_main)
/ / define motion array
Final String [] arraySport = {"football", "basketball", "tennis", "table tennis"}
Final boolean [] arraySportSelected = new boolean [] {false,false,false,false}
/ / instantiate AlertDialog dialog box
Dialog alertDialog = new AlertDialog.Builder (this)
.setTitle ("which sports do you like") / / set the title
.setIcon (R.drawable.ic_launcher) / / set icon
/ / the setup dialog box displays a check List, specifies the default selection, and sets the listening event handling.
.setMultiChoiceItems (arraySport, arraySportSelected, new DialogInterface.OnMultiChoiceClickListener () {
@ Override
Public void onClick (DialogInterface dialog, int which, boolean isChecked) {
/ / the Boolean authenticity of the selected item is saved to the selected item variable
ArraySportSelected [which] = isChecked
}
})
/ / add OK button and add listening processing
.setPositiveButton ("OK", new DialogInterface.OnClickListener () {
@ Override
Public void onClick (DialogInterface dialog, int which) {
StringBuilder sb = new StringBuilder ()
For (int iTuno Bandi)
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.