Alertdialog of Androidui Components

Source: Internet
Author: User

Package com.gc.alertdialogdemo;/** * Alertdialog: * 1, Alertdialog function is very powerful, it can generate a variety of content dialog box, actually Alertdialog generated * The dialog box can always be divided into 4 regions: (1) the icon area (2) the title area (3) content area (3) button area. * 2. Creating a dialog requires the following steps: * (1) Use to create a Alertdialog.builder object. * (2) Call Alertdialog.builder's Settitle () or Setcustomtitle () method to set the title * (3) to call Alertdialog.builder's SetIcon () method set Title * (4) Call the Alertdialog.builder Settings dialog box contents * (5) Call Alertdialog.builder's Setpositivebutton (), Setnegativebutton () or the Setneutralbutton () method to add multiple buttons * (6) Call the Create () method of the Alertdialog.builder creation Alertdialog object, and then call the Alertdialog object's show () method to display the dialog box. * 3, Alertdialog provides the following 6 ways to specify the contents of the dialog box: * (1) setmessage (): Set the dialog content as simple text content. * (2) Setitems (): Set dialog box contents as simple list item * (3) Setsinglechoiceitems (): Set dialog box content as a single-selection list item * (4) Setmultichoiceitems (): Set dialog box content as multi-select list item * (5) Setadapter (): Set dialog box content as custom list item * (6) Setview (): Set dialog box content as Custom view. * */import Android.os.bundle;import Android.app.activity;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.content.dialoginterface;import Android.content.dialoginterface.onclicklistener;impoRT Android.view.menu;import Android.view.view;import Android.widget.arrayadapter;import android.widget.TableLayout  Import android.widget.textview;/** * * @author Android General * */public Class Mainactivity extends Activity {public TextView Show;private string[] items=new string[]{"Android General", "General iOS", "General Cocos2d-x", "General"}; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); show= ( TextView) Findviewbyid (r.id.show);} public void CustomView (View source) {tablelayout loginform= (tablelayout) Getlayoutinflater (). Inflate (R.layout.login, NULL), New Alertdialog.builder (this). Settitle ("Custom View dialog box")//Settings icon. SetIcon (R.drawable.ic_launcher). Setview ( LoginForm). Setpositivebutton ("Login", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method stub}}). Setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stub}}). Create (). Show (); public void Customlist (View source) {Alertdialog.builder builder=new Alertdialog.builder (this)//Set Dialog caption. Settitle (" Custom List dialog box ")//Settings icon. SetIcon (R.drawable.ic_launcher). Setadapter (New arrayadapter<string> (This, R.layout.array_ Item, items), null); Setpositivebutton (builder); Setneutralbutton (builder);//Add "Cancel" to Alertdialog.builder button Setnegativebutton (builder). Create (). Show (); public void Multichoice (View source) {Alertdialog.builder builder=new Alertdialog.builder (this)//Set Dialog caption. Settitle (" Multi-select list dialog box ")//Settings icon. SetIcon (R.drawable.ic_launcher). Setmultichoiceitems (items, new boolean[]{false,true,false,true}, NULL); Setpositivebutton (builder); Setneutralbutton (builder);//Add "Cancel" button for Alertdialog.builder Setnegativebutton ( Builder). Create (). Show ();} public void Singlechoice (View source) {Alertdialog.builder builder=new Alertdialog.builder (this)//Set Dialog caption. Settitle (" Radio List dialog box ")//Settings icon. SetIcon (R.drawable.ic_launcher). Setsinglechoiceitems (items, 1, new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stubshow.settext ("You have selected" +items[ which]+ ("");}); Setpositivebutton (builder); Setneutralbutton (builder);//Add "Cancel" button Setnegativebutton (builder) for Alertdialog.builder. Create (). Show ();} public void SimpleList (View source) {Alertdialog.builder builder=new Alertdialog.builder (this)//Set Dialog caption. Settitle (" Simple List dialog box//Set icon. SetIcon (R.drawable.ic_launcher)//Set Simple list item contents. setitems (Items, new Onclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stubshow.settext ("You have selected" + items[which]+ ("");}); Setpositivebutton (builder); Setneutralbutton (builder);//Add "Cancel" button Setnegativebutton (builder) for Alertdialog.builder. Create (). Show ();} public void Easy (View source) {Alertdialog.builder builder=new Alertdialog.builder (this)//Set Dialog caption. settitle (Simple dialog box )//Set icon. SetIcon (R.drawable.ic_launcher). Setmessage ("Test contents of dialog box \ n second line");//Add "OK" for Alertdialog.builder button Setpositivebutton (builder); Setneutralbutton (bUilder);//Add "Cancel" button Setnegativebutton (builder) to Alertdialog.builder. Create (). Show (); Private builder Setneutralbutton (Builder Builder) {//TODO auto-generated method Stubreturn Builder.setneutralbutton ("  Modifier ", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stubshow.settext ("Clicked the Retouch button!");});} Private builder Setnegativebutton (Builder Builder) {//Call Setnegativebutton method to add "OK" button return Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Meth OD stubshow.settext ("Click the Cancel button!");});} Private builder Setpositivebutton (Builder Builder) {//Call Setpositivebutton method to add "OK" button return Builder.setpositivebutton ("OK", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Meth OD stubshow.settext ("Click OK" button! ");}});}}




Several layout files are as follows:
Activity_main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" > <textview android:id= "@+id/show" android:layout_width= "Fill_parent" a ndroid:layout_height= "Wrap_content"/> <button android:onclick= "simple" android:layout_w        Idth= "Fill_parent" android:layout_height= "Wrap_content" android:text= "Show simple dialog box"/> <button android:onclick= "SimpleList" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" an droid:text= "Simple List Item dialog box"/> <button android:onclick= "Singlechoice" android:layout_width= "Fill_pare NT "android:layout_height=" Wrap_content "android:text=" single-Selection list item dialog box "/> <button android:oncli ck= "Multichoice" android:layout_width= "fiLl_parent "android:layout_height=" wrap_content "android:text=" multi-select list item dialog box "/> <button androi d:onclick= "Customlist" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:t       ext= "Custom list Item dialog box"/> <button android:onclick= "CustomView" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:text= "Custom View dialog box"/></linearlayout>


Login.xml

<?xml version= "1.0" encoding= "Utf-8"? ><tablelayout xmlns:android= "http://schemas.android.com/apk/res/    Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:id=" @+id/loginForm " android:orientation= "vertical" > <TableRow> <textview android:layout_width= "Fill_par            Ent "android:layout_height=" wrap_content "android:text=" User name: "android:textsize=" 10pt "            /> <!--Enter a text box for the user name--<edittext android:layout_width= "Fill_parent"            android:layout_height= "wrap_content" android:hint= "please fill in the login account" android:selectallonfocus= "true"            /> </TableRow> <TableRow> <textview android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:text= "Password:" android:textsize= "10pt"/ > <!--text box to enter a password--       <edittext android:layout_width= "fill_parent" android:layout_height= "Wrap_content"        Android:hint= "Please fill in Password" android:password= "true"/> </TableRow> <TableRow>            <textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content"        android:text= "Phone number:" android:textsize= "10pt"/> <!--Enter the text box for the phone number--            <edittext android:layout_width= "fill_parent" android:layout_height= "Wrap_content"     Android:hint= "Please fill in your phone number" android:selectallonfocus= "true" android:phonenumber= "true"/> </TableRow></TableLayout>



Array_item.xml

<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent ">    </ Textview>


Partial effect:



Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/25739587

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.