Introduction to Android Development Alertdialog Usage Summary

Source: Internet
Author: User

For the development of the message you may be unfamiliar, that is, after clicking some buttons, will give the user a feedback message, this is the function of the prompt message, good message can give users a good user experience, so we today's learning goal is the correct use of the prompt box Alertdialog.

Import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.content.intent;import Android.net.uri;import Android.os.bundle;import Android.view.View;import Android.widget.button;public class Dialogdemoactivity extends Activity {private Button button;protected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.dialog); button = (button) Findviewbyid (R.id.button1); Button.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {Alertdialog.builder Builder = new Alertdialog.builder (dialogdemoactivity.this); Builder.settitle ("Message"). Setmessage ("Are you sure you want to surf the internet?"). Setcancelable (False). Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog,int ID) {Intent Intent = new Intent (Intent.action_view,uri.parse ("http://www.haosou.com")); StartActivity (intent);}}). Setnegativebutton ("Cancel", new Dialoginterface.onclicklisteneR () {@Overridepublic void OnClick (dialoginterface dialog,int id) {dialog.cancel ();}}); Alertdialog dialog = Builder.create ();d ialog.show ();}});}}
The above code can be used to implement the basic message, below we will explain the above features in detail.

Alertdialog.builder builder=new Alertdialog.builder (xxxactivity.this);

This is a builder object that creates an inner class of the Alertdialog class, and the main function parameter configuration requires this class to be implemented, and the methods commonly used are as follows.

Builder.settitle ("Message");
This method is used to set the caption of the prompt message.

Builder.setmessage ("Are you sure you want to surf the Internet?" ");
This method is to set the specific content of the prompt message.

Builder.setcancelable (FALSE);
This method is to set the Back button on the keyboard to take effect, set Fasle after the Back button expires, set true after the back button takes effect.

Builder.setpositivebutton ("OK", event);
Adds a OK button and adds a handle event to the button.

Builder.setnegativebutton ("Cancel", event);
Adds a Cancel button and adds a handling event to the button.

The above method can be flexibly set the message you want, but the display of the message may not be ideal, is the default effect, no particularly good effect, if you want to have a good effect, then you need to make the appropriate style settings.

Introduction to Android Development Alertdialog Usage Summary

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.