Android Alertdialog dialog Box Usage Example _android

Source: Internet
Author: User

This example describes the Android Alertdialog dialog box usage. Share to everyone for your reference, specific as follows:

Introduction to the Alertdialog dialog box

1, get alertdialog static internal class Buidler object, the class to create Alertdialog objects, because the Alertdialog construction method is all protected type
2, through the Buidler object to set the dialog box title, button and button to respond to the event Dialoginterface.onclicklistener
3, call the Buidler Create () method Creation dialog box
4, call the Alterdialog Show () method to display the content

Mode one: HTML-like Confirm dialog box

Alertdialog dialog box public
void Createalertdialog () {
  Alertdialog.builder Builder = new Alertdialog.builder (this );
  Builder.setmessage ("Test Alertdialog");
  Builder.setpositivebutton ("Yes", new Dialoginterface.onclicklistener () {
  @Override public
  void OnClick ( Dialoginterface dialog, int which) {
      toast.maketext (Getapplicationcontext (), "Delete succeeded", Toast.length_short). Show ();
    }
  });
  Builder.setnegativebutton ("No", new Dialoginterface.onclicklistener () {
  @Override public
  void OnClick ( Dialoginterface dialog, int which) {
      toast.maketext (Getapplicationcontext (), delete Cancel, toast.length_short). Show () ;
    }
  });
  Builder.create (). Show ();


Mode two: Set multiple entry options like context menu

//alertdialog set entries for multiple options or radio public void Createatheralertdialog () {//due to the construction of Alertdialog
   method is protected type, so you cannot directly new this object, you must rely on Alertdialog.builder class Alertdialog.builder Builder = new Alertdialog.builder (this);
   Builder = builder.settitle ("Alert similar to Menu");
   String[] Array = {"Roast wing", "Abalone", "Bear's Paw", "rubbish"};
   boolean[] numbers = {False,false,false,false}; The annotated way is to implement the radio//Builder.setitems (array, new Dialoginterface.onclicklistener () {//@Override//public void OnC
  Lick (dialoginterface dialog, int which) {//SYSTEM.OUT.PRINTLN (which);//}//}); The following way is to implement multiple-selection builder.setmultichoiceitems (array, numbers, new Dialoginterface.onmultichoiceclicklistener () {@Overri  De public void OnClick (Dialoginterface dialog, int which, Boolean ischecked) {System.out.println ("which
    : "+which +" ischecked: "+ ischecked";
   }
   });
Builder.create (). Show (); }

For more information on Android-related content readers can view the site topics: "Android File Operation tips Summary", "Android programming development of the SD card operation Summary", "Android Development introduction and Advanced Course", "Android Resources Operating Skills summary", " Android View tips Summary and a summary of the use of Android controls

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.