Android dialog box (Dialog) and custom Dialog

Source: Internet
Author: User

Activities provides a convenient and manageable dialog box mechanism for creating, saving, and replying, such as Oncreatedialog (int), onpreparedialog (int, Dialog), showDialog (int), Methods such as DismissDialog (int), if these methods are used, activity will return the activity-Managed Dialog (dialog) through the Getowneractivity () method. Oncreatedialog (int): When you use this callback function, the Android system effectively sets the activity to be the owner of each dialog box, thus automatically managing the status of each dialog box and attaching it to the activity. In this way, each dialog box inherits the specific properties of the activity. For example, when a dialog box opens, the menu key is displayed as a menu of options defined by the activity, and the volume key modifies the audio stream used by the activity. ShowDialog (int): When you want to display a dialog box, call the showDialog (int id) method and pass an integer that uniquely identifies the dialog box. When the dialog box is first requested, Android calls Oncreatedialog (int id) from your activity, and you should initialize this dialog box here dialog. This callback method is passed the same ID as the ShowDialog (int id). When you create this dialog box, the object is returned at the end of the activity. Onpreparedialog (int, Dialog): Android also calls the optional callback function onpreparedialog (int ID, Dialog) before the dialog box is displayed. You can define this method if you want to change any of its properties when the dialog box is opened. This method is called every time the dialog box is opened, and Oncreatedialog (int) is called only when the dialog box is opened for the first time. If you do not define Onpreparedialog (), then this dialog will remain the same as it was last opened. This method is also passed as the ID of the dialog box, and the dialog object created in Oncreatedialog (). DismissDialog (int): When you are ready to close the dialog box, you can eliminate it by calling dismiss () on the dialog box. If necessary, you can also invoke the dismissdialog (int id) method from this activity, which will actually call the dismiss () method for you to this dialog box. If you want to use oncreatedThe ialog (int id) method manages the State of your dialog box (as discussed in the previous section), and then each time your dialog is eliminated, the status of the dialog object is retained by the activity. If you decide that you no longer need this object or clear the state is important, then you should call Removedialog (int id). This will remove any internal object references and if this dialog box is being displayed, it will be eliminated.
Instance:
public class Mainactivity extends Activity implements View.onclicklistener {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Button BT1 = (button) Findviewbyid (R.ID.BT1); Button BT2 = (button) Findviewbyid (R.ID.BT2); Button BT3 = (button) Findviewbyid (R.ID.BT3); Button BT4 = (button) Findviewbyid (R.ID.BT4); Button Bt5 = (button) Findviewbyid (R.ID.BT5); Button Bt6 = (button) Findviewbyid (R.ID.BT6); Button bt7 = (button) Findviewbyid (R.ID.BT7); Bt1.setonclicklistener (this); Bt2.setonclicklistener (this); Bt3.setonclicklistener (This), Bt4.setonclicklistener (This), Bt5.setonclicklistener (this); Bt6.setonclicklistener ( this); Bt7.setonclicklistener (this);} protected void Dialog () {Alertdialog.builder builder = new Builder (mainactivity.this); Builder.setmessage ("Confirm exit?"). "); Builder.settitle (" hint "); Builder.setpositivebutton (" Ack ", new Onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Dialog.dismiSS (); MainActivity.this.finish ();}}); Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Dialog.dismiss ();}}); Builder.create (). Show (); protected void Dialog2 () {Dialog Dialog = new Alertdialog.builder (this). SetIcon (Android. R.drawable.btn_star). Settitle ("Survey of Preferences"). Setmessage ("Do you like the movie of the Week Star?"). "). Setpositivebutton (" Very like ", New Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {// TODO auto-generated Method Stubtoast.maketext (Mainactivity.this, "I like his movie very much. ", Toast.length_long). Show ();}}). Setnegativebutton ("Don't like", New Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO Auto-generated method Stubtoast.maketext (Mainactivity.this, "I don't like his films." ", Toast.length_long). Show ();}}). Setneutralbutton ("General", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO Au To-generated method Stubtoast.maketext (Mainactivity.this, "I don't like it." ", Toast.lenGth_long). Show ();}}). Create ();d ialog.show ();} protected void Dialog3 () {new Alertdialog.builder (this). Settitle ("Please enter"). SetIcon (Android. R.drawable.ic_dialog_info). Setview (New EditText (This)). Setpositivebutton ("OK", null). Setnegativebutton ("Cancel", null ). Show ();} protected void Dialog4 () {new Alertdialog.builder (this). Settitle ("check box"). Setmultichoiceitems (new string[] {"Item1", " Item2 "}, Null,null). Setpositivebutton (" OK ", null). Setnegativebutton (" Cancel ", null). Show (); protected void Dialog5 () {new Alertdialog.builder (this). Settitle ("Radio Box"). SetIcon (Android. R.drawable.ic_dialog_info). Setsinglechoiceitems (new string[] {"Item1", "Item2"}, 0,new Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog,int which) {Dialog.dismiss ();}}). Setnegativebutton ("Cancel", null). Show (); protected void Dialog6 () {new Alertdialog.builder (this). Settitle ("list box"). Setitems (new string[] {"Item1", "Item2"}, NULL ). Setnegativebutton ("OK", null). Show (); protected void Dialog7 () {Layoutinflater Inflater =Getlayoutinflater (); View layout = Inflater.inflate (R.layout.dialog, (ViewGroup) Findviewbyid (R.id.dialog)); new Alertdialog.builder (This) . Settitle ("Custom Layout"). Setview (Layout). Setpositivebutton ("OK", null). Setnegativebutton ("Cancel", null). Show (); @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.bt1:dialog (); break;case r.id.bt2:dialog2 (); Case R.id.bt3:dialog3 (); break;case r.id.bt4:dialog4 (); break;case r.id.bt5:dialog5 (); break;case R.id.bt6:dialog6 (); Break;case R.id.bt7:dialog7 (); break;default:break;}}

Layout file
<linearlayout 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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" Android:o rientation= "vertical" tools:context= "com.example.dialogtest.MainActivity" > <button android:id= "@+id/bt 1 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" OK Cancel Dialog "/&        Gt <button android:id= "@+id/bt2" android:layout_width= "wrap_content" android:layout_height= "Wrap_cont Ent "android:text=" Three buttons dialog box "/> <button android:id=" @+id/bt3 "android:layout_width=" Wrap_ Content "Android:layout_height=" Wrap_contenT "android:text=" dialog box with text box "/> <button android:id=" @+id/bt4 "android:layout_width=" Wrap_c Ontent "android:layout_height=" Wrap_content "android:text=" Radio dialog box "/> <button android:id= "@+id/bt5" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Check pair Box "/> <button android:id=" @+id/bt6 "android:layout_width=" Wrap_content "Android:layout_ height= "wrap_content" android:text= "List dialog box"/> <button android:id= "@+id/bt7" Android:layo Ut_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Custom dialog box"/&GT;&LT;/LINEARLAYOUT&G T

Run results



Finish

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android dialog box (Dialog) and custom Dialog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.