android-dialog box

Source: Internet
Author: User

First, the common dialog box

Alertdialog: The most versatile and most widely used dialog box (the following three dialog boxes are subclasses of the dialog box)

ProgressDialog: Progress dialog box, this dialog box is just the packaging of the progress bar

Datepickerdialog: Date selection dialog, this dialog box is only for DatePicker packaging

Timepickerdialog: The Time Selection dialog box, which is the wrapper for Timepicker

Ii. Creating a dialog box with Alertdialog

Steps:

1, create Alertdialog.builder object;

2. Call Alertdialog.builder's Settitle () or Setcustomtitle () method to set the caption;

3, call Alertdialog.builder's SetIcon () method to set the icon;

4, call Alertdialog.builder related methods to set the content, Alertdialog specified the following 6 ways to specify the contents of the dialog box:

<1>setmessage () Setting dialog box content is simple text;

<2>setitems () Sets the contents of the dialog box to a simple list item;

<3>setsinglechoiceitems () Sets the content of the dialog box to be a single-selection list item;

<4>setmultichoiceitems () Sets the content of the dialog box to be a multi-select list item;

<5>setadapter () Sets the contents of the dialog box to a custom list item;

<6>setview () Settings dialog box content for custom view;

5, call Alertdialog.builder Setpositivebutton (), Setnegativebutton (), Setneutralbutton () method to add multiple buttons;

6. Call the Create () method of Alertdialog.builder. Alertdialog object, and then call the show () method of the Alertdialog object to display the dialog box.

eg.

public void simple (View source) {

Builder Builder=new Builder (this);
Setting the dialog box title
Builder.settitle ("Simple dialog box");
Set icon
Builder.seticon (R.drawable.ic_launcher);
Builder.setmessage ("The content of the test in the dialog box \ n the second line");
Add button
Builder.setpositivebutton ("OK", new Onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {

Tv.settext ("Click OK" button);
}
});
Builder.setnegativebutton ("Cancel", new Onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {

Tv.settext ("Click the Cancel button");
}
});
Builder.create ();
Builder.show ();

}

Execution Result:

Three, Simple List item dialog box

Call Alterdialog.builder's Setitems () method to set the Simple List Item dialog box, which needs to pass in an array or array resource ID when calling the method;

public void simple (View source) {

Builder Builder=new Builder (this);
Setting the dialog box title
Builder.settitle ("Simple List Item dialog box");
Set icon
Builder.seticon (R.drawable.ic_launcher);
Builder.setitems (items, new Onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {

Tv.settext ("You have selected" +items[which]+ ");
}
});
Add button
Builder.setpositivebutton ("OK", new Onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {

Tv.settext ("Click OK" button);
}
});
Builder.setnegativebutton ("Cancel", new Onclicklistener () {

@Override
public void OnClick (Dialoginterface dialog, int which) {

Tv.settext ("Click the Cancel button");
}
});
Builder.create ();
Builder.show ();

}

Iv. single-selection list item dialog box

android-dialog box

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.