Use of dialog (Android review)

Source: Internet
Author: User


I. Basic points of knowledge

The common Dialog


Basic code:
Alertdialog.builder Builder = new Alertdialog.builder (this);
Alertdialog dialog = Builder.create ();
Dialog.show ();


1) Common dialog box
Builder.setmessage ("Browse the website of the Preach Intelligence Podcast");
Builder.setpositivebutton



2) Select dialog box
Builder.setitems (items, new Dialoginterface.onclicklistener ()

Items is an array of option contents



3) Single-selection dialog box
Builder.setsinglechoiceitems (items, 0, new Dialoginterface.onclicklistener ()


Second parameter: The default selected button



4) Multi-Select dialog box
Builder.setmultichoiceitems (items, CheckedItems, new Dialoginterface.onmultichoiceclicklistener ()



5) Customize dialog box
Layoutinflater Minflater = Layoutinflater.from (this);
View view = Minflater.inflate (R.layout.custom_dialog, NULL);


Builder.setview (view);





Second, the sample code

1, Mainactivity

Package Com.example.dialogtest;import Android.os.bundle;import Android.app.activity;import android.app.AlertDialog ; Import Android.content.dialoginterface;import Android.content.dialoginterface.onclicklistener;import Android.content.dialoginterface.onmultichoiceclicklistener;import Android.view.layoutinflater;import Android.view.menu;import Android.view.view;import Android.widget.button;import Android.widget.Toast;public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main);} public void CommonDialog (view view) {Alertdialog.builder Builder = new Alertdialog.builder (this); builder.setcancelable (false); Builder.settitle ("Goddess"); Builder.setmessage ("Zetian is your Goddess?"); Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {}}); Builder.setneutralbutton ("Hidden", new Onclicklistener () {@Overridepublic void OnClick (Dialoginterface dialog,int which) {//TODO auto-generated Method stub}}); Builder.setnegativebutton ("Cancel", new Onclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which) {}}); Alertdialog dialog = Builder.create ();d ialog.show ();} public void Selectdialog (view view) {Alertdialog.builder Builder = new Alertdialog.builder (this); final String items[] = NE W string[]{"Bleeding Day", "Bleeding Day 1", "Zetian 2", "Zetian 3"};builder.settitle ("select Goddess"); Builder.setitems (items, new Onclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which) {Toast.maketext (Getapplicationcontext (), Items[which], 0). Show ();}); Alertdialog dialog = Builder.create ();d ialog.show ();}    public void Singleselectdialog (view view) {Alertdialog.builder Builder = new Alertdialog.builder (this); Builder.settitle ("select Goddess"); final String items[] = new string[]{"Bleeding Day", "Bleeding Day 1", "Zetian 2", "Zetian 3"}; Builder.setsinglechoiceitems (items, 0, new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Toast.maketext (GetapplicationcontExt (), Items[which], 0). Show ();}}); Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {}}); Alertdialog dialog = Builder.create ();d ialog.show ();} public void Multiselectdialog (view view) {Alertdialog.builder Builder = new Alertdialog.builder (this); final String items [] = new string[]{"Bleeding Day", "Bleeding days 1", "Zetian 2", "Zetian 3",};final boolean checkeditems[] = new Boolean[]{true,false,false,false}; Builder.settitle ("select Goddess"); Builder.setmultichoiceitems (items, CheckedItems, new Onmultichoiceclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which, Boolean isChecked) {Toast.maketext ( Getapplicationcontext (), Items[which], 0). Show ();}}); Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {}}); Alertdialog dialog = Builder.create ();d ialog.show ();} public void Customdialog (view view) {Layoutinflater Inflater = Layoutinflater.from (this); View MView = Inflater.inflaTE (r.layout.custom_dialog, null); Alertdialog.builder Builder = new Alertdialog.builder (this), Builder.settitle ("Please enter user information"); Builder.setview (MView);    Final Alertdialog Dialog = Builder.create ();    Dialog.show (); Button okbtn = (button) Mview.findviewbyid (R.ID.BT_OK); Okbtn.setonclicklistener (new View.onclicklistener () {//Note, This demo uses two Onclicklistener. So here you need to add view@overridepublic void OnClick (View v) {Dialog.dismiss ();}});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}


Third, the source code download

http://download.csdn.net/detail/caihongshijie6/7799073


Four





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.