Dialogs are an integral part of Android, and you need to use the Alertdialog.builder class when using a dialog box. Of course, the System Default dialog box, you can also customize the dialog box, if the dialog box set the button, then you want to listen to the event Onclicklistener.
The following is an instance of a Alertdialog.builder class and a custom dialog box, when clicked OK, transfer to the Login dialog box, after entering the user name and password, transfer to the Login Progress dialog box
Here the custom dialog box is composed of two TextView and two edittext, that is, the Login dialog box, the layout file of the custom dialog is Dialog.xml file, see below
In addition, using Alertdialog to create a dialog, you need to know a few ways
Settitle (); Set a caption for a dialog box
SetIcon (); Set the icon for the dialog box
Setmessage (); Setting the dialog box's prompt information
Setitems (); Sets a list to display for a dialog box, typically used to display several commands
Setsinglechoiceitems (); The Settings dialog box displays a list of the selected
Setmultichoiceitems (); The Settings dialog box displays a series of check boxes
Setpositivebutton (); Add the "Yes" button to the dialog box
Setnegativebutton (); Add the "No" button to the dialog box
Dialogtest.java
Package Org.hualang.dialog;import Android.app.activity;import Android.app.alertdialog;import android.app.Dialog; Import Android.app.progressdialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.view;public class Dialogtest extends Activity {/** called when the Act Ivity is first created. */ProgressDialog Mydialog; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Dialog dialog=new Alertdialog.builder (dialogtest.this). Settitle ("Login Prompt")//Set caption. setmessage ("Login required here")//Settings dialog box Setpositivebutton (OK,//Set OK button new Dialoginterface.onclicklistener () {@Overri De public void OnClick (Dialoginterface dialog, int which) {//click OK to turn Login dialog Box Layoutinflater factory=layoutinflater.from (DiAlogtest.this); Get the custom dialog box final View dialogview=factory.inflate (r.layout.dialog, NULL); Create dialog box Alertdialog dlg=new alertdialog.builder (dialogtest.this) . Settitle ("Login Box"). Setview (Dialogview)//Set custom dialog box styles . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {//Set listener events @Override public void OnClick (Dialoginterface dialog, int which) {//Enter finish and click "OK" to start the login Mydialog=progressdialog.show (Dialogtest.this, "Please wait ...", "Logging in ...", true); New Thread () { public void Run () { try { Sleep (3000); }catch (Exception e) { E.printstacktrace (); }finally { Login end, Cancel Mydialog dialog box M Ydialog.dismiss (); } } }.start (); }}). Setnegativebutton ("Cancel",//Set Cancel button new Dialoginter Face. Onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {//click to cancel back Out program DialogTest.this.finish (); }). Create ();//Creating dialog Box Dlg.show ();//Display dialog box }}). Setneutralbutton ("Exit", New Dialoginterface.onclicklisten ER () {@Override public void OnClick (Dialoginterface dialog, int which) { Click Exit after exiting the program DialogTest.this.finish (); }). Create ();//Creates a button//Display dialog box Dialog.show (); }}
Dialog.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><textview android:id= "@+id/username" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:layout_marginleft=" 20dip "android:layout_marginright=" 20dip "android:text=" username "Android:gravi Ty= "left" android:textappearance= "? Android:attr/textappearancemedium"/><edittext android:id= "@+id/user Name "android:layout_width=" Fill_parent "android:layout_height=" Wrap_content "Android:layout_marginle ft= "20dip" android:layout_marginright= "20dip" android:scrollhorizontally= "true" android:autotext= "Fal Se "android:capitalize=" none "android:gravity=" Fill_horizontal "android:textappearance="? android:attr /textappearancemedium "/><textView android:id= "@+id/password" android:layout_width= "wrap_content" android:layout_height= "Wrap_conte NT "android:layout_marginleft=" 20dip "android:layout_marginright=" 20dip "android:text=" password "and" Roid:gravity= "left" android:textappearance= "Android:attr/textappearancemedium"/><edittext android:id= "@+id/password" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:layou t_marginleft= "20dip" android:layout_marginright= "20dip" android:scrollhorizontally= "true" Android:aut Otext= "false" android:capitalize= "None" android:gravity= "Fill_horizontal" android:password= "true" Android:textappearance= "? Android:attr/textappearancemedium"/></linearlayout>
The results of the operation are as follows:
When you click OK, you will be redirected to the Login dialog box, which is a custom dialog box.
After entering the user name and password, click OK, enter the dialog with the progress bar, where the dialog box with the progress bar is the system default, and with out-of-the-box control, 3 seconds after the end of the dialog, and jump to the corresponding activity
Supplemental content:
1, inflater English means expansion, in Android is the meaning of expansion. The
Layoutinflater acts like Findviewbyid (), and the difference is that Layoutinflater is used to find the XML layout file under the Layout folder and instantiate it! Findviewbyid () is to find specific widget controls (such as Button,textview, etc.) under a specific XML. It can be used in many places, such as Baseadapter's GetView, custom dialog to get widget widgets in view, and so on.
2, Alertdialog.builder is the meaning of the warning dialog
3, Unit
px: Is the pixel point of the screen
in: Inch
mm: MM
pt: lbs, 1/ 72-inch
DP: An abstract unit based on density, if a 160dpi screen, 1dp=1px
Dip: Equivalent to DP
SP: similar to DP, but also scaled according to the user's font size preference.
Use SP as the unit of text, other dip
4, dialog.xml description
①android:layout_marginleft= "20dip"
margin is the meaning of the edge, The above statement says that the control is 20 dips from the left. The same
android:layout_marginright= "20dip" is the control from the parent control to the right of 20dip
②android:gravity= "left": indicates that the control's text text is displayed on the leftmost
③android:layout_gravity= "Center": indicates that the control is in the middle of the parent control
④android:textappearance use
For controls that can display text, such as the TextView EditText RadioButton Button checkbox, you sometimes need to control the size of the font. The Android platform defines three font sizes.
"? Android:attr/textappearancelarge" ""? Android:attr/textappearancemedium ""? Android:attr/textappearancesmall "
Use the following method:
Android:textappearance= "? Android:attr/textappearancelarge" android:textappearance= "? android:attr/ Textappearancemedium "android:textappearance="? Android:attr/textappearancesmall "
Or
Style= "? Android:attr/textappearancelarge" style= "? Android:attr/textappearancemedium" style= "? android:attr/ Textappearancesmall "
⑤android:scrollhorizontally= "true": if the text exceeds the width of the TextView, the horizontal pull bar appears
⑥android:autotext= "false": if set, the spelling correction of the input value will be performed automatically. There is no effect here, it works when the input method is displayed and entered. Set to false here to turn off sub-kinetic energy
⑦android:capitalize= "None": Sets the English letter capitalization type. No effect here, need to pop input method to see
⑧android:password= "true": with small dots "." Display text for entering a password
The above is the Android UI Control series: Dialog (dialog box) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!