Android Interface Programming--dialog box control
2.5 dialog box control
A dialog box is a small window that prompts the user to make a decision or enter additional information. The dialog box does not populate the screen and is typically used for modal events that require the user to take action to continue execution. Common dialog-related controls in Android are Toast,alertdialog,Popupwindow , ProgressDialog wait
2.5.1 Toast
Toast is used to pop up a simple text message to provide feedback to the app, to remain visible and interactive with the current Activity , and to disappear automatically after a certain amount of time. Toast there are two forms of basic Toast and Custom Toast , the effect 2.5.1-1 as shown:
Figure 2.5.1-1
1, the basic Toast Implementation
First, a Toastis instantiated by Maketext () and then called Show () to display Code implementation:
Toast Toast = Toast.maketext (context, "Please check network Connections",
Toast.length_short.);
Toast.show ();
2. Custom Toast
If a simple text message is not enough, you can customize the layout for the Toast and set the display position on the screen.
? To create a custom layout, define the layout of the view
? Load the layout with the setview() method.
The following is a case 2.5-1to achieve The application effect shown in 2.5-1
Figure 2.5-1
2.5.2 Alertdialog
Alertdialog is a subclass of Dialog that enables simple, convenient, and flexible build dialogs. Alertdialog can display a caption, up to three buttons, a list of selectable items, or a custom layout. Alertdialog
There are three main layout areas as shown in 2.5-2:
Figure 2.5-2
Alertdialog Implementation Essentials
? Implement Add button to Alertdialog
Add a button to the dialog box by calling the Setpositivebutton () and Setnegativebutton () methods, and the code is as follows :
Alertdialog.builder Builder = new Alertdialog.builder (getactivity ());
Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int id) {
}
});
Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
public void OnClick (Dialoginterface dialog, int id) {
}});
? Create a custom layout
To have a custom layout for a dialog box, create a layout and then add it to The Alertdialog.builder object by Setview () Alertdialog, the code is as follows:
Alertdialog.builder Builder = new Alertdialog.builder (getactivity ());
Layoutinflater inflater = getactivity (). Getlayoutinflater ();
Builder.setview (inflater.inflate (r.layout.dialog_signin, null));
By default, custom layouts populate the dialog window, but you can still use the alertdialog.builder method to add buttons and captions.
The following is a case 2.5-2to achieve The application effect shown in 2.5-3
Figure 2.5-3
2.5.3 Popupwindow
Popupwindow is a dialog box that pops up anywhere, displaying a floating container at the top of the currently active activity. The difference between Alertdialog and Popupwindow:
? The position of the alertdialog is fixed, while the position of the Popupwindow can be arbitrary.
? Alertdialog are non-blocking threads, while popupwindow are blocking threads.
Implementation of Popupwindow:
? Build Popupwindow
View view= Layoutinflater.from (this). Inflate (
R.layout.popuwindow_layout,null);
Mpopupwindow=new Popupwindow (view);
? Set Popupwindow
1. Click the outside area to close the Popupwindow
After displaying Popupwindow, click outside to close the window to make the following settings, otherwise clicking outside does not turn off Popupwindow.
Mpopupwindow.setfocusable (TRUE);
Colordrawable colordrawable=new colordrawable (0);
Mpopupwindow.setbackgrounddrawable (colordrawable);
2. Add animations for Popupwindow
First define the animation resource in Anim, define the style in style and then use the Setanimationstyle () method to animate Popupwindow, as follows:
Enter the animation:
<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<alpha
Android:fromalpha= "0"
Android:toalpha= "1"
android:duration= "1000"
/>
</set>
Exit Animation:
<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<alpha android:duration= "1000"
Android:fromalpha= "1" android:toalpha= "0"/>
</set>
To define a style:
<style name= "Popuanimationstyle" >
<item name= "Android:windowenteranimation" > @anim/popup_enter
</item>
<item name= "Android:windowexitanimation" > @anim/popup_out</item>
</style>
Set animation
Mpopupwindow.setanimationstyle (R.style.popuanimationstyle);
? Eject Popupwindow
Showasdropdown (view): relative to the position of a control (just below the left), no offset
Showasdropdown (view, Xoff, Yoff): Offset relative to the position of a control
Showasdropdown (view, Xoff, Yoff, Gravity): offset relative to the specified position of a control
Showatlocation (parent, gravity, X, y): Can be set offset or no offset relative to the parent control's specified position (for example, central Gravity.center, Lower gravity.bottom, etc.)
The following is a case 2.5-3to explain The use of Popupwindow
1.Implement the popup menu directly below the control
2.5.3 ProgressDialog
A dialog box with a progress bar (Progressbar) that displays only one text or view plus a progress bar, but the text and view cannot be used at the same time. If an indeterminate progress (circular progress bar) is displayed, ProgressDialog is not recommended and should instead be used in the activity layout
Several methods of ProgressDialog
1.setMax ()
sets the maximum value of the progress bar in the dialog box, with a value range of 0. 10000.
2.setTile ()
Set the caption.
3.setProgressStyle ()
Sets the style of the progress bar in the dialog box. For example: annular and horizontal.
Parameters:
Progressdialog.style_spinner Ring Precision Bar.
Progressdialog.style_horizontal the horizontal style of the progress bar.
4.setMessage ()
Sets the contents of the display.
The following is a case 2.5-4, explaining The use of progressdialog, the implementation of the effect 2.5-4
Figure 2.5-4
Tip: For asynchronous tasks in this example, see the use of Asynctask see chapter III Event handling asynchronous task-related content
public class Progressdialogactivity extends Appcompatactivity {private ProgressDialog progressdialog; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_progress_dialog); Showprogressdialog (); New Updateasynctask (). Execute (); } private void Showprogressdialog () {//create ProgressDialog ProgressDialog = new ProgressDialog (Progressdialo Gactivity.this); Sets the style of the ProgressDialog Progressdialog.setprogressstyle (progressdialog.style_horizontal); Set the title Progressdialog.settitle ("Software Update"); Set the content to be displayed progressdialog.setmessage ("Downloading"); Set the maximum value of Progressdialog.setmax (100); Set the progress value progressdialog.setprogress (0); Show dialog box Progressdialog.show (); } private Integer progress=0; Private class Updateasynctask extends asynctask<void,integer,void>{@Override protected VoID doinbackground (Void ... params) {//Analog network operation. while (progress<100) {try {thread.sleep (1000); progress+=10; Publishprogress (progress); } catch (Interruptedexception e) {e.printstacktrace (); }} return null; } @Override protected void OnPostExecute (void aVoid) {//After execution completes, close the dialog box. Progressdialog.dismiss (); } @Override protected void Onprogressupdate (Integer ... values) {log.d ("Jereh", "dfdfdf"); Super.onprogressupdate (values); Change the value of the progress bar progressdialog.setprogress (Values[0]); } }
Code reference:
Codes\ch02\2.5\customdialog\app\src\main\java\com\jerehedu\progressdialogactivity.java
The peak of the cupola 20160709
Android Interface Programming--dialog Control (iv)