---restore content starts---
There are two types of Android dialogs: Popupwindow and Alterdialog
Their differences are: alterdialog position fixed (Hint dialog: PS for a moment to understand this first), and Popupwindow position can be arbitrary (gradually display the window:p s also according to their own understanding of it)
Alertdialog need to build a creator Alertdialog.builder Bulider
Alertdialog.builder bulider=new Alertdialog.builder (alertactivity.this);
Some functions of the alterdialog are accomplished by the Bulider object, for example:
Bulider.settitle ("hint");//The caption of the prompt dialog box
Bulider.seticon (R.mipmap.ic_launcher);//The picture displayed
Bulider.setmessage ("Are you sure to do this?" ");//Prompt information
and to set the interface to interact with the user method for the user to operate. The implementation method is as follows:
Bulider.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (dialoginterface dialog, int i) {
Toast.maketext (Getbasecontext (), "OK", Toast.length_short). Show ();
}
});
Bulider.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Toast.maketext (Getbasecontext (), "Cancel", Toast.length_short). Show ();
}
});
Alterdialog also provides a single-selection dialog box and a multi-select dialog box (PS: Personally think that these two dialog boxes are quite fun, specific fun reasons I do not know ...). Hee Hee
Single-selection dialog box:
@Override
public void OnClick (View v) {
Alertdialog.builder bulider=new alertdialog.builder (Alertac Tivity.this);
Bulider.setcancelable (FALSE);
Final string[]items={"Beijing", "Shanghai", "Yantai"};
Bulider.setsingle choiceitems (items, 1, new Dialoginterface.onclicklisten ER () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
Toast.maketext (Getbasecontext (), items[which].tostring (), Toast.length_short). Show ();
Dialog.dismiss ();
}
});
Alertdialog alertdialog = Bulider.create ();
Alertdialog.show ();
}
});
Here are a few questions that I have encountered:
1.bulider.setcancelable (false);//Do not know this sentence is what to use t_t.
2. When you write your own code, you don't notice that items are set to the final constant
3.dialog,dismiss ();//function is to cancel the dialog box
4. At the end of the method, remember to create bulider:bulider.create ();
Multi-Select dialog box:
Bt3.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Alertdialog.builder bulider=new Alertdialog.builder (alertactivity.this);
Bulider.setcancelable (FALSE);
Bulider.settitle ("Choose Your City");
Final string[] items={"Beijing", "Shanghai", "Yantai"};
Final boolean[] boo={false,false,false};//why to set three Boolean values
Bulider.setMultiChoiceitems (items, NULL, new Dialoginterface.onmultichoiceclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which, Boolean isChecked) {
if (isChecked) {
Toast.maketext (Getbasecontext (), items[which].tostring (), Toast.length_short). Show ();
Boo[which]=true;
}else{
Boo[which]=false;
}
}
});
Bulider.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (Dialoginterface dialog, int which) {
StringBuffer SF = new StringBuffer ();
for (int i = 0; i < items.length; i++) {
if (boo[i] = = True)
Sf.append (Items[i] + "");
}
Toast.maketext (Getbasecontext (), sf.tostring (), Toast.length_short). Show ();
Dialog.dismiss ();
}
});
Alertdialog alertdialog=bulider.create ();
Alertdialog.show ();
}
});
Multiple selection dialog box and Radio dialog box The overall structure is still the same, just a few more options, more checkbox options
Problems encountered:
1.final boolean[] boo={false,false,false};//Why to set three Boolean values
A: This should be set to the default state, without the selected city. However, the following implementation methods are: Bulider.setmultichoiceitems (items, NULL, new Dialoginterface.onmultichoiceclicklistener ()
Passed a null, since there is no argument, why do you want to set the default state of the city is not selected?
Bt4.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
View view=getlayoutinflater (). Inflate (R.layout.toast_layout,null);
ImageView IV = (ImageView) View.findviewbyid (R.ID.TOAST_IV);
Iv.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Toast.maketext (Alertactivity.this, "click picture", Toast.length_short). Show ();
}
});
Alertdialog.builder bulider=new Alertdialog.builder (alertactivity.this);
Bulider.setview (view);
Alertdialog alertdialog=bulider.create ();
Alertdialog.show ();
}
});
Purely personal Beginner's understanding (the wrong place also wants to point out): Layoutinflater Its function gives me a feeling somewhat similar to Findviewbyid (). The difference is that Layoutinflater is used to find the XML file under res/layout/, and achieve
However, it is not found in the XML file and is somewhat abstract. The view in the above program is loaded into the memory space via Layoutinflater and is no longer an XML file. And Findviewbyid is looking for specific controls under the XML layout file (such as Button,textview)
layoutinflater its specific function:
1. Layoutinflater.inflater () is needed for an XML file that has not been written to, but wants to be loaded, or wants to get and use his interface.
2. For a layout interface that is written to an XML file and wants to get and use it, you can use the Findviewbyid () method to get the layout controls in it
Popupwindow:
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_pop_window);
bt1= (Button) Findviewbyid (R.ID.BT1);
View V=getlayoutinflater (). Inflate (r.layout.toast_layout,null);
Final Popupwindow pw=new Popupwindow (v);
Pw.setfocusable (TRUE);//Set the state of focus
Colordrawable cd=new colordrawable ();
Pw.setbackgrounddrawable (CD);
Pw.setanimationstyle (R.style.pop);//Set the style of the animation
Bt1.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
Pw.setwidth (View.getwidth ());
int height=getresources (). Getdisplaymetrics (). Heightpixels/3;
Pw.setheight (height);
Pw.showasdropdown (view, 0, 0);//Set Popwindow display position at the bottom of view, X y for coordinate offset
Pw.showatlocation (view, Gravity.top | Gravity.right, 0, 170);//Location of the display
}
});
}
Attention:
1.pw.setfocusable (False), indicates that Popupwindow cannot get the focus, even if setting the background is not empty and cannot click outside to disappear, only by dismiss () disappear. But outside the view event can still trigger, back key also can smooth dismiss
Popupwindow summarized as follows:
1. The view layout for Popupwindow
View v=getlayoutinflater (). Inflate (R.layout.toast_layout,null);
2. Display location, use the Showasdropdown method
3. The Setanimationstyle method is used to enter the animation.
4. Vanishing with Dismiss ()?
---restore content ends---
Summary of the prompt dialog box