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