Original URL: http://www.cnblogs.com/xiaoluo501395377/p/3421727.html
Lower right corner Focus Button recommended buttons to support Oh if you have any questions about the content of the article, you can contact me by comment or email: [Email protected] /[email protected] If you need to reprint, please specify the source, thank you!!
This essay will continue to learn about the dialog in the Android UI family, including Datepickerdialog, Timepickerdialog, toasts, and ProgressDialog. Let's take a look at the use of these different dialog dialog boxes.
First, Datepickerdialog and Timepickerdialog
Datepickerdialog and Timepickerdialog are Android-provided dialog boxes that pop up a date, time selection, We can get a date, Time dialog box by instantiating Datepickerdialog and Timepickerdialog in the program. These two classes are Alertdialog subclasses:
Java.lang.Object ? Android.app.Dialog ? Android.app.AlertDialog ? Android.app.DatePickerDialogjava.lang.Object ? Android.app.Dialog ? Android.app.AlertDialog ? Android.app.TimePickerDialog
The use of these two controls is very simple, so let's take a look at the use of Datepickerdialog and Timepickerdialog directly in the following example:
We define two buttons on the layout and two EditText, when clicking on the first button, pop up a datepickerdialog, and get the selected date displayed on the first EditText, click on the second button, pop up a timepickerdialog , the time to be selected is displayed on the second edittext.
Activity_main.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "> <Butt On android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" android:layout_margintop = "99DP" android:text= "Datepickerdialog"/> <edittext android:id= "@+id/edittext1" Android:lay Out_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparenttop= "true" an Droid:layout_centerhorizontal= "true" android:layout_margintop= "29DP" android:ems= "ten" > </edittext& Gt <edittext android:id= "@+id/edittext2" android:layout_width= "Wrap_content" android:layout_height= "W Rap_content "Android:layout_below= "@+id/button1" android:layout_centerhorizontal= "true" android:layout_margintop= "31DP" android:ems= "Ten" > <requestfocus/> </EditText> <button android:id= "@+id/button2" Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_below= "@+id/editText2" Android:layout_centerhorizontal= "true" android:layout_margintop= "24DP" android:text= "Timepickerdialog"/& Gt;</relativelayout>
Mainactivity class:
public class Mainactivity extends activity{private button button; Private Button button2; Private EditText EditText; Private EditText editText2; private int year, monthofyear, DayOfMonth, hourofday, minute; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button = (button) Findviewbyid (R.id.button1); Button2 = (Button) Findviewbyid (R.id.button2); EditText = (editText) Findviewbyid (R.ID.EDITTEXT1); EditText2 = (EditText) Findviewbyid (R.ID.EDITTEXT2); Calendar calendar = Calendar.getinstance (); Year = Calendar.get (calendar.year); Monthofyear = Calendar.get (calendar.month); DayOfMonth = Calendar.get (calendar.day_of_month); Hourofday = Calendar.get (Calendar.hour_of_day); minute = Calendar.get (Calendar.minute); Button.setonclicklistener (New Onclicklistener () { @Override public void OnClick (view view) {/** * instantiates a datep Ickerdialog Object * The second parameter is a Datepickerdialog.ondatesetlistener anonymous inner class, when the user chooses a good date click Done to invoke the Ondateset method inside */Datepickerdialog Datepickerdialog = new Datepickerdialog (mainactivity.this, New Datepickerdialog. Ondatesetlistener () {@Override public void Ondateset (DatePicker view , int year, int monthofyear, int dayofmonth) {Editte Xt.settext ("Date:" + year + "-" + (Monthofyear + 1) + "-" + dayofmonth); }}, year, Monthofyear, DayOfMonth); Datepickerdialog.show (); } }); Button2.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { /** * Instantiate an Timepickerdialog object * The second parameter is an Timepickerdialog.ontimesetlistener anonymous inner class, when the user chooses a good time and clicks done will Call inside the Ontimeset method */Timepickerdialog Timepickerdialog = new Timepickerdialog (mainactivity.t His, new Timepickerdialog.ontimesetlistener () {@Override public void Ontimeset (timepicker view, int hourofday, int minute) {Edittext2.settext ("time : "+ Hourofday +": "+ minute); }}, Hourofday, minute, true); Timepickerdialog.show (); } }); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the AC tion Bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
We see that if we want to get a date or time popup, just instantiate a datepickerdialog and Timepickerdialog object, let's see what the date and time popup looks like:
When you click Done, the bindings to the listener are called separately to get the time we set
Second, Toasts
Toasts dialog box is also very simple, in fact, in some of the previous essays we have seen a lot of use of toast this dialog box to pop up a message to our users, to note that toast box is not able to interact with the user, when more than we set the time, The Toast dialog box disappears automatically, and we can instantiate a default Toast object directly using the Maketext of toast, which is a static method
public static Toast Maketext (context context, charsequence text, int. duration) Parameterscontext the context to use . U Sually your application or Activity object.text the text to show. Can is formatted text.duration how long to display the message. Either Length_short or Length_long
The first parameter specifies the window under which the Toast box pops up, the second parameter specifies the text content of the popup box, and the third parameter specifies the time that the popover appears.
The default toast popup is a black box that pops up at the bottom of the screen, and we can also set our own custom toast popup and can set the popup position by Setgravity ().
Here we also look at the use of toast boxes using an example:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <button android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" android:layout_margintop= "32DP" android:text= "pop up a regular toast box"/> <button android:id= "@ +id/button2 " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android: layout_below= "@+id/button1" android:layout_centerhorizontal= "true" android:layout_margintop= "29DP" android:text= "Pop up a custom toast box"/></relativelayout>
Also define two buttons, when Clicked Button, pops up a toast box
Button.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { // Instantiate a Toast dialog box Toast toast = Toast.maketext (Mainactivity.this, "Hello World", Toast.length_short); / * * Setgravity method to set the pop-up position of the toast box, the first parameter specifies the orientation of the popup, the second parameter specifies the offset of the x-axis * The third parameter specifies the offset of the y-axis */ Toast.setgravity (gravity.center, 0, 0); } );
We see that when we set the Setgravity () method, the toast's popup position will change according to our settings.
Button2.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { // load Our custom XML layout file with the Layoutinflater method View view = Layoutinflater.from (Mainactivity.this). Inflate (r.layout.toast, null); get a Toast object directly by new method toast toast = new Toast (mainactivity.this); Set the popup content of our toast box to our custom toast toast.setview (view); Toast.show (); } });
We can also pop a custom toast box by customizing an XML layout file and then using the Setview method of the toast.
Third, ProgressDialog
Finally, let's take a look at ProgressDialog, which is a pop-up box for the progress bar, which is also a subclass of Alertdialog
Java.lang.Object ? Android.app.Dialog ? Android.app.AlertDialog ? Android.app.ProgressDialog
ProgressDialog can display a pop-up box with a progress bar that contains a text message or a custom view. Note: There can only be one text message and a custom view.
If we want to create a progressdialog, we can create it directly from new, because the progress bar has two styles, one is the horizontal progress bar, the other is the circle progress bar, so we can also set the display style of the progress bar popup. Let's take a look at the progress bar popup for each of these two styles by using an example:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <button android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" android:layout_margintop= "42DP" android:text= "pop up a circular progress bar"/> <button android:id= "@+id/ Button2 " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android: layout_alignright= "@+id/button1" android:layout_below= "@+id/button1" android:layout_margintop= "44DP" android:text= "Pop up a horizontal progress bar box"/> </RelativeLayout>
A circular progress bar box:
Button.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { // Instantiate a ProgressDialog ProgressDialog ProgressDialog = new ProgressDialog (mainactivity.this); Progressdialog.settitle ("hint message"); Progressdialog.setmessage ("Downloading, please later ..."); sets the display style of the ProgressDialog, Progressdialog.style_spinner represents a circular progress bar Progressdialog.setprogressstyle ( Progressdialog.style_spinner); Progressdialog.show (); } });
We can click on the other part of the screen, and then this progress bar popup will disappear, if you want it to not disappear can call
Progressdialog.setcancelable (false); setting popup cannot be canceled
Take a look at the Horizontal progress bar box:
Button2.setonclicklistener (New Onclicklistener () { @Override public void OnClick (View v) { // Instantiate a ProgressDialog ProgressDialog ProgressDialog = new ProgressDialog (mainactivity.this); Progressdialog.settitle ("hint message"); Progressdialog.setmessage ("Downloading, please later ..."); set the maximum progress, the ProgressDialog progress range is from 1-10000 Progressdialog.setmax (); Set the master Progress progressdialog.setprogress (); set the second progress progressdialog.setsecondaryprogress (.); sets the display style of the ProgressDialog, Progressdialog.style_horizontal represents the horizontal progress bar Progressdialog.setprogressstyle ( Progressdialog.style_horizontal); Progressdialog.show (); } });
These are the two ways to use the progress bar
Summary: This article casually mainly explains the Datepickerdialog, Timepickerdialog, toasts and progressdialog four kinds of pop-up box use method, in the following essays, will continue to record the learning of Android dots and drops.
The "Go" Android UI family-----Time, date, toasts, and progress Bar dialog