Android Development Portal dialog box simple usage _android

Source: Internet
Author: User
Tags sqlite database

This example describes the simple use of the Android Introduction dialog box. Share to everyone for your reference, specific as follows:

Note: This article is just a learning note to record where you have learned

1. Get alertdialog static internal Class Builder object, this class to create a dialog box
2. Setting the caption button of the dialog box and the event that the button responds to through the builder object
3. Invoke the Create () method of the Builder dialog box
4. Call Alertdialog Show () method to display the dialog box

Main.xml file

<?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/mytextview" android:layout_width= "
  fill _parent "
  android:layout_height=" wrap_content "
  android:text=" @string/hello "
  />
<button
  android:id= "@+id/mybutton"
  android:layout_width= "fill_parent"
  android:layout_height= "WRAP_" Content "
  android:text=" Create alert dialog box
  />
</LinearLayout>

Mainactivity file

@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Mytextview = (TextView) Findviewbyid (R.id.mytextview);
    MyButton = (Button) Findviewbyid (R.id.mybutton);
    Add Alertdialog.builder object Final Alertdialog.builder Builder = new Alertdialog.builder (this); Adds a button event Mybutton.setonclicklistener (new View.onclicklistener () {@Override public void onclic for the button in the activity
      K (View v) {builder.settitle ("Are you sure you want to delete this message?"). Set OK button Setpositivebutton ("Yes", new Onclicklistener () {@Override public void OnClick (Dialog
        Interface dialog, int which) {mytextview.settext ("delete succeeded");
      }
      }). Set the Cancel button Setnegativebutton ("No", new Onclicklistener () {@Override public void OnClick (Dialogi
        Nterface dialog, int which) {Mytextview.settext ("cancel Delete");
       }
      });
        Create a dialog boxAlertdialog Alertdialog = Builder.create ();
    Display dialog box Alertdialog.show ();
  }
    });

 }
}

For more information on Android-related content readers can view the site topics: "The Android Control Usage Summary", "Android Resource Operation skills Summary", "Android File Operation skills Summary", "Android operation SQLite Database Skills Summary", " Android operation JSON format data tips summary, "Android Database Operation skills Summary", "Android programming SD Card Operation method Summary", "Android Development introduction and Advanced Course", "Android programming activity Operation Skill Summary And the overview of Android View tips

I hope this article will help you with the Android program.

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.