Learn android from scratch (Dialog box. 25 .)

Source: Internet
Author: User

Learn android from scratch (Dialog box. 25 .)
In the graphic interface, the dialog box is also an important form of human-computer interaction. The program can prompt users with some information through the dialog box, you can also perform some simple interaction operations with the program through the dialog box. During Android development, all the dialogs are inherited from the android. app. Dialog class. The Inheritance structure is as follows: java. lang. Object? Android. app. Dialog
Common DIalog Methods

1 Public void setTitle (CharSequence title) Normal Set the display title of the dialog box
2 Public void setTitle (int titleId) Normal Set the title of the dialog box. The content is specified for the resource file.
3 Public void show () Normal Show dialog box
4 Public void hide () Normal Hide dialog box
5 Public boolean isShowing () Normal Check whether the dialog box is displayed
6 Public void setContentView (View view) Normal Set View Components
7 Public void setContentView (int layoutResID) Normal Set the View component ID
8 Public void dismiss () Normal Hide dialog box
9 Public void closeOptionsMenu () Normal Close option menu
10 Public void setDismissMessage (Message msg) Normal Set the message in the hide dialog box
11 Public void setCancelable (boolean flag) Normal Whether the setting can be canceled
12 Public void setCancelMessage (Message msg) Normal Set the message when the dialog box is canceled
13 Public void cancel () Normal The cancel dialog box is similar to the dismiss () method.
14 Public Window getWindow () Normal Retrieve Window object
15 Public void setOnShowListener (DialogInterface. OnShowListener listener) Normal Listener when the Setting dialog box is opened
16 Public void setOnDismissListener (DialogInterface. OnDismissListener listener) Normal Listener when the dialog box is hidden
17 Public void setOnCancelListener (DialogInterface. OnCancelListener listener) Normal Set dialog box to cancel listening

In general, we use the internal class in AlertDialog to instantiate the dialog object.
You can understand the commonly used methods in DIalog. The details are described in the Code.
1 Public AlertDialog. Builder (Context context) Structure Create AlertDialog. Builder object
2 Public AlertDialog. Builder setMessage (int messageId) Normal Set the resource ID of the displayed information
3 Public AlertDialog. Builder setMessage (CharSequence message) Normal Set the string of the display information
4 Public AlertDialog. Builder setView (View view) Normal Set the displayed View component
5 Public AlertDialog. Builder setSingleChoiceItems (CharSequence [] items, int checkedItem, DialogInterface. OnClickListener listener) Normal The Setting dialog box displays a single-choice List, specifying the selected items by default, and setting the listener processing operation.
6 Public AlertDialog. Builder setSingleChoiceItems (ListAdapter adapter, int checkedItem, DialogInterface. OnClickListener listener) Normal The Setting dialog box displays a single-choice List, specifying the selected items by default, and setting the listener processing operation.
7 Public AlertDialog. Builder setMultiChoiceItems (CharSequence [] items, boolean [] checkedItems, DialogInterface. OnMultiChoiceClickListener listener) Normal The Setting dialog box displays a checklist and sets the listener processing operation.
8 Public AlertDialog. Builder setPositiveButton (CharSequence text, DialogInterface. OnClickListener listener) Normal Add a confirmation button for the dialog box and set the listening operation
9 Public AlertDialog. Builder setPositiveButton (int textId, DialogInterface. OnClickListener listener) Normal Add a confirmation button for the dialog box. The displayed content is specified by the resource file and the listener operation is set.
10 Public AlertDialog. Builder setNegativeButton (CharSequence text, DialogInterface. OnClickListener listener) Normal Set a Cancel button for the dialog box and set the listener operation
11 Public AlertDialog. Builder setNegativeButton (int textId, DialogInterface. OnClickListener listener) Normal Set a Cancel button for the dialog box. The displayed content is specified by the resource file and the listener operation is set.
12 Public AlertDialog. Builder setNeutralButton (CharSequence text, DialogInterface. OnClickListener listener) Normal Set a common button and a listener
13 Public AlertDialog. Builder setNeutralButton (int textId, DialogInterface. OnClickListener listener) Normal Set a common button to display the content specified by the resource file and set the listening operation
14 Public AlertDialog. Builder setItems (CharSequence [] items, DialogInterface. OnClickListener listener) Normal Set the information content as a list item and set the listening operation
15 Public AlertDialog. Builder setItems (int itemsId, DialogInterface. OnClickListener listener) Normal Sets the information content as a list item. The content of the list item is specified by the resource file, and sets the listening operation.
16 Public AlertDialog create () Normal Create an instantiation object of AlertDialog
17 Public AlertDialog. Builder setIcon (Drawable icon) Normal Set the displayed icon
18 Public AlertDialog. Builder setIcon (int iconId) Normal Set the resource ID of the icon to be displayed



I understand the AlertDialog method. Let's take a look at the example.

1 * the simplest dialog box * button1
2 ****** dialog box with buttons ******** button2
3 ****** dialog box with event ******** button3
4 ****** dialog box with options ******** button4
5 * single-choice dialog box * button5
6 * button6
* With progress bar dialog box * button7
8 * Date selection dialog box * button8
9 * Event Selection dialog box * button9
10 * User-defined dialog box * button10


See the code below
Main XML file
     
      
      
      
      
      
      
      
      
      
  
 

Custom M. xml
 
     
          
           
       
      
          
           
   ?
  
 

JAVA files
Package com. example. dialog; import android. app. activity; import android. app. alertDialog; import android. app. datePickerDialog; import android. app. datePickerDialog. onDateSetListener; import android. app. dialog; import android. app. progressDialog; import android. app. timePickerDialog; import android. app. timePickerDialog. onTimeSetListener; import android. content. dialogInterface; import android. OS. bundle; import android Oid. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. datePicker; import android. widget. timePicker; import android. widget. toast; public class MainActivity extends Activity {private Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button10; private String CityData [] = {"Henan", "Beijing", "Shanghai", "Tianjin", "Hunan", "Shenzhen"}; // city Data private String SexData [] = {"male", "female "}; // gender data private String ColorData [] = {"red", "orange", "yellow", "green", "blue", "Purple "}; // color data private boolean ColorChoiced [] = {false, false}; // sets the default option @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // instantiate button1 = for the button (Button) this. findViewById (R. id. button1); button2 = (Button) this. findViewById (R. id. button2); button3 = (Button) this. findViewById (R. id. button3); button4 = (Button) this. findViewById (R. id. button4); button5 = (Button) this. findViewById (R. id. button5); button6 = (Button) this. findViewById (R. id. button6); button7 = (Button) this. findViewById (R. id. button7); button8 = (Button) this. findViewById (R. id. button8); B Utton9 = (Button) this. findViewById (R. id. button9); button10 = (Button) this. findViewById (R. id. button10); // The simplest dialog box, button1.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubDialog dialog = new AlertDialog. builder (MainActivity. this ). setTitle ("About Us") // set the title. setMessage ("Feiyang software Co., Ltd.") // sets the prompt information. setIcon (R. drawable. a2) // set the icon. create (); // create dialog box dial Og. show (); // display dialog box}); // Information Processing dialog box with buttons button2.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubDialog dialog = new AlertDialog. builder (MainActivity. this ). setTitle ("delete information") // set the title. setMessage ("are you sure you want to delete this message? ") // Set the prompt information. setIcon (R. drawable. a2) // set the icon. setNegativeButton ("cancel", null) // sets the cancel button. setPositiveButton ("OK", null) // sets the OK button. create (); // create dialog box dialog. show (); // display dialog box}); // The button3.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View view) dialog box with Event Processing) {// TODO Auto-generated method stubDialog dialog = new AlertDialog. builder (MainActivity. this ). setTitle ("Exit program "). setMessage ("are you sure you want to exit the program? "). SetIcon (R. drawable. a2 ). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialogInterface, int whitch) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "the program is about to exit", 2 ). show (); // finish (); In order to demonstrate the effect, do not actually exit, use the toast instead }}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int Arg1) {// TODO Auto-generated method stub }}). create (); dialog. show () ;}}); // dialog box list button4.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubDialog dialog = new AlertDialog. builder (MainActivity. this ). setTitle ("select your employment City "). setIcon (R. drawable. a2 ). setNegativeButton ("cancel", null ). setItems (CityData, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dInterface, int whitch) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "you selected:" + CityData [whitch], 2 ). show ();}}). create (); dialog. show () ;}}); // The button5.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubDialog dialog = new AlertDialog. builder (MainActivit Y. this ). setTitle ("select your gender "). setSingleChoiceItems (SexData, 0, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dInterface, int whitch) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "you selected:" + SexData [whitch], 2 ). show ();}}). setIcon (R. drawable. a2 ). setNegativeButton ("cancel", null ). setPositiveButton ("OK", null ). create (); dialog. show () ;}}); // dialog box with multiple selection boxes, Ton6.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubDialog dialog = new AlertDialog. builder (MainActivity. this ). setTitle ("select your preferred color "). setMultiChoiceItems (ColorData, ColorChoiced, new DialogInterface. onMultiChoiceClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int whitch, boolean isChecked) {// TODO Auto-ge Nerated method stubToast. makeText (MainActivity. this, "you selected" + ColorData [whitch], 2 ). show (); // No judgment is made here, so this event is triggered whether selected or deselected. Interested readers can set IsChecked judgment }}). setIcon (R. drawable. a2 ). setNegativeButton ("cancel", null ). setPositiveButton ("OK", null ). create (); dialog. show () ;}}); button7.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubfinal Progress Dialog dialog = new ProgressDialog (MainActivity. this); dialog. setMessage ("loading information. Please wait! "); Dialog. setTitle ("loading"); dialog. setIcon (R. drawable. a2); dialog. onStart (); new Thread () {@ Overridepublic void run () {// TODO Auto-generated method stubtry {Thread. sleep (3*1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {dialog. dismiss ();}}}. start (); dialog. show () ;}}); // Date selection dialog box button8.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubDatePickerDialog dialog = new DatePickerDialog (MainActivity. this, new OnDateSetListener () {@ Overridepublic void onDateSet (DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "the set time is:" + year + "year" + (monthOfYear + 1) // Add 1 to month, the original month starts from scratch + "month" + dayOfMonth + "day", 2 ). show () ;},2014, 8, 17); dialog. show () ;}}); // Time Selection dialog box button9.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubTimePickerDialog dialog = new TimePickerDialog (MainActivity. this, new OnTimeSetListener () {@ Overridepublic void onTimeSet (TimePicker arg0, int hourOfDay, int minute) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "set the time to:" + hourOfDay + "Hour" + minute + "minute", 2 ). show () ;}, 20, 55, true); dialog. show () ;}}); // custom dialog box button10.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubView myView = LayoutInflater. from (getApplication ()). inflate (R. layout. custom, null); // converts the layout object to the VIew object Dialog dialog = new AlertDialog. builder (MainActivity. this ). setTitle ("User Login "). setIcon (R. drawable. a2 ). setNegativeButton ("cancel", null ). setPositiveButton ("login", null ). setView (myView) // set the View component to be displayed. create (); dialog. show ();}});}}

Returns the corresponding button effect in sequence.














Through the single-choice and multi-choice dialog boxes, you can design a lot of user interaction, progress bars, threads, and asynchronous operations to process time-consuming operations. The custom dialog box greatly increases the diversity of the UI, it is possible to better feedback interaction.

Next prediction: ImageSwitcher image switching component

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.