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
- View view = getLayoutInflater (). inflate (R. layout. dialog,Null); // Load the interface layout corresponding to R. layout. popup
- FinalPopupWindow popup =NewPopupWindow (view, 500,300); // specify the layout resource, window height, and length
- // Popup. showAsDropDown (view); // PULL mode shown below
- Popup. showAtLocation (findViewById (R. id. dialogBtn), Gravity. CENTER, 100,100 );
- 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
- Calendar calendar = Calendar. getInstance ();
- NewDatePickerDialog (MainActivity.This,NewDatePickerDialog. OnDateSetListener (){
- Public VoidOnDateSet (DatePicker view,IntYear,IntMonthOfYear,IntDayOfMonth ){
- Toast. makeText (MainActivity.This, "The date you selected is:" + year + ":" + monthOfYear + ":"
- + DayOfMonth, Toast. LENGTH_SHORT). show ();
- }
- }
- // Initialization date
- , Calendar. get (Calendar. YEAR)
- , Calendar. get (Calendar. MONTH)
- , Calendar. get (Calendar. DAY_OF_MONTH). show ();
- /*************************************** *********************************/
- Calendar calendar = Calendar. getInstance ();
- NewTimePickerDialog (MainActivity.This,NewOnTimeSetListener (){
- Public VoidOnTimeSet (TimePicker view,IntHourOfDay,IntMinute ){
- Toast. makeText (MainActivity.This, "The time you selected is:" + hourOfDay + ":" + minute, Toast. LENGTH_SHORT). show ();
- }
- }
- // Initialization time
- , Calendar. get (Calendar. HOUR_OF_DAY)
- , Calendar. get (Calendar. MINUTE ),
- // True indicates that the 24-hour format is used.
- 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
- Public ClassMainActivityExtendsActivity {
- @ Override
- Protected VoidOnCreate (Bundle savedInstanceState ){
- Super. OnCreate (savedInstanceState );
- SetContentView (R. layout. main );
- // Progress bar
- ProgressDialog progressDialog =Null;
- Final Static IntMAX_PROGRESS = 100;
- Private Int[] Data =New Int[60];
- IntProgressStatus = 0;
- IntHasData = 0;
- }
- /**
- * Define a Handler responsible for updating the progress bar
- **/
- Handler handler =NewHandler (){
- Public VoidHandleMessage (Message msg ){
- If(Msg. what = 0x110 ){
- ProgressDialog. setProgress (progressStatus );
- }
- };
- };
- Public VoidDialogBtn (View v ){
- /**
- * Create a progress bar dialog box
- **/
- ProgressDialog =NewProgressDialog (This); // Instantiate a ProgressDialog object
- ProgressDialog. setTitle ("task in progress ");
- ProgressDialog. setMessage ("the task is being executed. Please wait ");
- ProgressDialog. setCancelable (False); // The Setting dialog box can be closed through the touch screen.
- ProgressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); // set the progress bar style of the dialog box.
- ProgressDialog. setIndeterminate (False); // Set whether the progress bar of the dialog box displays the progress bar.
- ProgressDialog. show (); // display dialog box
- /**
- * Create a subthread and send messages to Handler periodically.
- **/
- NewThread (NewRunnable (){
- Public VoidRun (){
- While(ProgressStatus <MAX_PROGRESS ){
- ProgressStatus = MAX_PROGRESS * doWork ()/data. length; // gets the percentage of completed time-consuming operations.
- Handler. sendEmptyMessage (0x110 );
- }
- If(ProgressStatus> = MAX_PROGRESS) {// the task is completed. Exit the dialog box.
- ProgressDialog. dismiss ();
- }
- }
- }). Start ();
- }
- /**
- * Simulate a time-consuming operation
- **/
- Protected IntDoWork (){
- Data [hasData ++] = (Int) (Math. random () * 100 );
- Try{
- Thread. sleep (500); // 0.5s updated once
- }Catch(InterruptedException e ){
- E. printStackTrace ();
- }
- ReturnHasData;
- }
- }
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