Android Common Four kinds of dialog box examples to explain _android

Source: Internet
Author: User

1, dialog box notification (Dialog Notification)

When your application needs to display a progress bar or require the user to confirm the information, you can use the dialog box to complete it.

The following code opens a dialog box as shown in the illustration:

public void Click1 (view view) {
Alertdialog.builder Builder = new Builder (this);
Builder.settitle ("Hall No. 1th");
Builder.seticon (r.drawable.ic_launcher);
Builder.setmessage ("Browse Wuyudong's blog?");
Builder.setpositivebutton (OK), new Onclicklistener () {
@Override public
void OnClick (Dialoginterface dialog , int which) {
uri uri = uri.parse ("http://www.wuyudong.com/");//Open link
Intent Intent = new Intent (intent.action_ VIEW, URI);
StartActivity (intent);
}
);
Builder.setnegativebutton ("Cancel", new Onclicklistener () {
@Override public
void OnClick (dialoginterface dialog, int which) {
dialog.cancel ();
}
});
Alertdialog dialog = Builder.create ();
Dialog.show ();
}

2. Create a dialog box with a single list of options

public void Click2 (view view) {
Alertdialog.builder Builder = new Builder (this);
Builder.settitle ("Radio dialog Box");
Final string[] items = new string[] {"Java", ". Net", "PHP"};
Builder.setsinglechoiceitems (items, 0, new Onclicklistener () {
@Override public
void OnClick (dialoginterface dialog, int which) {
toast.maketext (mainactivity.this, Items[which] + "clicked", 0). Show
();
Builder.show ();
}

3. Create a dialog box with multiple options list

public void Click3 (view view) {
Alertdialog.builder Builder = new Builder (this);
Builder.settitle ("Multiple selection dialog box");
Final string[] items = new string[] {"Java", ". Net", "PHP", "C + +"};
Builder.setmultichoiceitems (items, new boolean[] {true, False, False,
true}, new Onmultichoiceclicklistener () {
    @Override public
void OnClick (dialoginterface dialog, int which,
Boolean ischecked) {
Toast.maketext ( Mainactivity.this, Items[which] + ischecked, 0)
. Show (();
}
);
Builder.setnegativebutton ("Cancel", new Onclicklistener () {
@Override public
void OnClick (dialoginterface dialog, int which) {
dialog.cancel ();
}
});
Builder.show ();
}

4. Progress dialog box (ProgressDialog)

Use code progressdialog.show (progressdialogactivity.this, "Wait a Minute", "Data is loading ...", true), and create and display a progress dialog box.
Call the Setprogressstyle () method to set the progress dialog style. There are two different styles:

Progressdialog.style_spinner style (default style)
Progressdialog.style_horizontal horizontal progress bar style public
void Click4 ( View view) {
ProgressDialog pd = new ProgressDialog (this);
Pd.settitle ("Reminder");
Pd.setmessage ("Loading ...");
Pd.show ();
}

The following code implements the horizontal progress bar style:

public void Click5 (view view) {
final progressdialog pd = new ProgressDialog (this);
Pd.setprogressstyle (progressdialog.style_horizontal);
Pd.setmax (m);
Pd.show ();
New Thread () {public
void run () {(
int i = 0; i < i++) {
pd.setprogress (i);
try {
thread.sleep;
} catch (Interruptedexception e) {
e.printstacktrace ()
}} Pd.dismiss ();
};
Start ();

The above is a small set to introduce Android common four kinds of dialog box examples to explain, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.