Android dialog box usage (2), android Usage Details

Source: Internet
Author: User

Android dialog box usage (2), android Usage Details
Blog: http://blog.csdn.net/u012637501/article/details/46003413
I. Use PopupWindow to create a dialog box-style windowThe procedure for creating a dialog box style window using PopupWindow is as follows:> call the PopupWindow constructor to create a PopupWindow object;> call showAsDropDown (View v) of PopupWindow to display PopupWindow as the drop-down component of the v component; or call the showAtLocation () method of PopupWindow to display PopupWindow at the specified position. > Call the dismiss () method of PopupWindow to cancel the display of the popup window;(1) dialog box style
(2) code implementation

  1. View view = getLayoutInflater (). inflate (R. layout. dialog,Null); // Load the interface layout corresponding to R. layout. popup
  2. FinalPopupWindow popup =NewPopupWindow (view, 500,300); // specify the layout resource, window height, and length
  3. // Popup. showAsDropDown (view); // PULL mode shown below
  4. Popup. showAtLocation (findViewById (R. id. dialogBtn), Gravity. CENTER, 100,100 );
  5. Popup. dismiss ();
Note: R. layout. dialog. xml only contains one ImageVIew and Button component. 2. Use the DatePickDialog and TimePickerDialog dialog boxesCreate a date and time dialog box using DataPickDialog and TimePickerDialog. The main steps are as follows:> Create a DatePickerDialog and TimePickerDialog instance using the new keyword and call their show () method to display the date and time selection dialog box.> bind a listener to DatePickerDialog and TimePickerDialog. You can set an event trigger listener to obtain the events you set. DatePickerDialog (Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) timePickerDialog (Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) 1. Dialog Box Style
2. Code Implementation
  1. Calendar calendar = Calendar. getInstance ();
  2.  NewDatePickerDialog (MainActivity.This,NewDatePickerDialog. OnDateSetListener (){
  3. Public VoidOnDateSet (DatePicker view,IntYear,IntMonthOfYear,IntDayOfMonth ){
  4. Toast. makeText (MainActivity.This, "The date you selected is:" + year + ":" + monthOfYear + ":"
  5. + DayOfMonth, Toast. LENGTH_SHORT). show ();
  6. }
  7. }
  8. // Initialization date
  9. , Calendar. get (Calendar. YEAR)
  10. , Calendar. get (Calendar. MONTH)
  11. , Calendar. get (Calendar. DAY_OF_MONTH). show ();
  12. /*************************************** *********************************/
  13. Calendar calendar = Calendar. getInstance ();
  14. NewTimePickerDialog (MainActivity.This,NewOnTimeSetListener (){
  15. Public VoidOnTimeSet (TimePicker view,IntHourOfDay,IntMinute ){
  16. Toast. makeText (MainActivity.This, "The time you selected is:" + hourOfDay + ":" + minute, Toast. LENGTH_SHORT). show ();
  17. }
  18. }
  19. // Initialization time
  20. , Calendar. get (Calendar. HOUR_OF_DAY)
  21. , Calendar. get (Calendar. MINUTE ),
  22. // True indicates that the 24-hour format is used.
  23. True). Show ();
3. Use ProgressDialog to create a progress bar dialog box You can use either of the following methods to create a progress dialog box:> to create a simple progress dialog box, call the static show () method provided by ProgressDialog to display the dialog box. ProgressDialog = progressDialog. show (LoginActivity. this, "", "is logged on. Please wait ..... "); progressDialog. dismiss (); // cancel progress bar Dialog Box> Create a ProgeressDialog, and then call the progress bar in the method dialog box to set it. After the setting is complete, the dialog box is displayed for processing. ProgressDialog contains the following common methods: setIndeteminate (boolean indeteminate): The progress bar in the Setting dialog box does not display the progress value. setMax (int max): sets the maximum value of the progress bar in the dialog box. setMax (int max ): set the maximum value of the progress bar in the dialog box; setMessage (CharSequence message): Set the message setProgress (int value) displayed in the dialog box: Set the progress value of the progress bar in the dialog box setProgressStyle (int style ): set the progress bar style in the dialog box 1. Dialog Box Style
2. Code Implementation
  1.  Public ClassMainActivityExtendsActivity {
  2. @ Override
  3. Protected VoidOnCreate (Bundle savedInstanceState ){
  4. Super. OnCreate (savedInstanceState );
  5. SetContentView (R. layout. main );
  6. // Progress bar
  7. ProgressDialog progressDialog =Null;
  8. Final Static IntMAX_PROGRESS = 100;
  9. Private Int[] Data =New Int[60];
  10. IntProgressStatus = 0;
  11. IntHasData = 0;
  12. }
  13. /**
  14. * Define a Handler responsible for updating the progress bar
  15. **/
  16. Handler handler =NewHandler (){
  17. Public VoidHandleMessage (Message msg ){
  18. If(Msg. what = 0x110 ){
  19. ProgressDialog. setProgress (progressStatus );
  20. }
  21. };
  22. };
  23. Public VoidDialogBtn (View v ){
  24. /**
  25. * Create a progress bar dialog box
  26. **/
  27. ProgressDialog =NewProgressDialog (This); // Instantiate a ProgressDialog object
  28. ProgressDialog. setTitle ("task in progress ");
  29. ProgressDialog. setMessage ("the task is being executed. Please wait ");
  30. ProgressDialog. setCancelable (False); // The Setting dialog box can be closed through the touch screen.
  31. ProgressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); // set the progress bar style of the dialog box.
  32. ProgressDialog. setIndeterminate (False); // Set whether the progress bar of the dialog box displays the progress bar.
  33. ProgressDialog. show (); // display dialog box
  34. /**
  35. * Create a subthread and send messages to Handler periodically.
  36. **/
  37. NewThread (NewRunnable (){
  38. Public VoidRun (){
  39. While(ProgressStatus <MAX_PROGRESS ){
  40. ProgressStatus = MAX_PROGRESS * doWork ()/data. length; // gets the percentage of completed time-consuming operations.
  41. Handler. sendEmptyMessage (0x110 );
  42. }
  43. If(ProgressStatus> = MAX_PROGRESS) {// the task is completed. Exit the dialog box.
  44. ProgressDialog. dismiss ();
  45. }
  46. }
  47. }). Start ();
  48. }
  49. /**
  50. * Simulate a time-consuming operation
  51. **/
  52.  Protected IntDoWork (){
  53. Data [hasData ++] = (Int) (Math. random () * 100 );
  54. Try{
  55. Thread. sleep (500); // 0.5s updated once
  56. }Catch(InterruptedException e ){
  57. E. printStackTrace ();
  58. }
  59. ReturnHasData;
  60. }
  61. }
Note: int [] data = new int [60]; it is because when the array size is 50, the array subscript out-of-bounds error occurs when the time-consuming operation module accumulates. Java. lang. ArrayIndexOutOfBoundsException: length = 50; index = 50

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.