Introduction to Android Development (i) detailed activities 1.4 Show normal dialog box

Source: Internet
Author: User

Sometimes, you may need to pop up a dialog box to get some confirmation from the user's input. In this case, you can override the protected method (protected) Oncreatedialog () in the activity base class.

1. Create a project: Dialog.

2. Code in the Main.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=     
"http://schemas.android.com/apk/" Res/android " 
    android:layout_width=" fill_parent " 
    android:layout_height=" fill_parent " 
  
    android:o" rientation= "vertical" >     
             
<button     
    android:id= "@+id/btn_dialog" android:layout_width= "Fill_" 
    Parent "  
    android:layout_height=" wrap_content "android:text=" click to 
    Display a dialog " 
    Android:onclick = "OnClick"/>     
          
</LinearLayout>

3. Code in the Dialogactivity.java.

public class Dialogactivity extends activity {charsequence[] items = {"Google", "Apple", "Microsoft"};     
         
    boolean[] itemschecked = new Boolean[items.length]; /** called the activity is a.     
        * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     
    Setcontentview (R.layout.main);     
    public void OnClick (View v) {showdialog (0);  @Override protected Dialog oncreatedialog (int id) {switch (ID) {case     
                    0:return New Alertdialog.builder (this). SetIcon (R.drawable.ic_launcher) . Settitle ("This are a dialog with some simple text ..."). Setpositivebutton ("OK" , new Dialoginterface.onclicklistener () {public voi D onClick (dialoginterface dialog, int whichbutton) { Toast.maketext (Getbasecontext (), "OK clicked!", toast.length     
                                _short). Show ();     
                            }). Setnegativebutton ("Cancel", New Dialoginterface.onclicklistener () {public void OnClick (Dialoginterface dialog , int whichbutton) {Toast.maketext (     
                                            Getbasecontext (), "Cancel clicked!",     
                                Toast.length_short). Show (); }). Setmultichoiceitems (iteMS, itemschecked, new Dialoginterface.onmultichoiceclicklistener () { public void OnClick (Dialoginterface dialog, int which, Boole     
                                            An ischecked) {Toast.maketext (     
                                                    Getbasecontext (), Items[which] + (ischecked?) 
                                                            "Checked!"     
         
                                : "unchecked!"), Toast.length_short). Show ();     
        ). Create ();     
    return null; }     
}

Related Article

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.