Android several different dialog boxes to implement the way _android

Source: Internet
Author: User

App must be a variety of dialog to interact with the user, and here are some tips for introducing a few prompt boxes.

Creating a dialog box typically takes the following steps:

1, create the Alertdialog.builder object.

2, call the Alertdialog.builder settitle () or Setcustomtitle () method to set the caption.

3, call the Alertdialog.builder SetIcon () method to set the title logo.

4, call Alertdialog.builder related methods to set the dialog box content.

5, call the Alertdialog.builder Setpositivebutton (), Setnegativebutton (), or Setneutralbutton () method to add more than one button.

6, call the Alertdialog.builder Create () method creates the Alertdialog object, and then call the Alertdialog object's show () method to display the dialog box.

Where the 4th step sets the contents of the dialog box, here are 6 ways to specify:

setmessage (): Sets the dialog box content to simple text content.

Setitems (): Sets the dialog box content to be a simple list item.

Setsinglechoiceitems (): Sets the dialog box content to be a radio list item.

Setmultichoiceitems (): Sets the dialog box content to be a multiple-selection list item.

Setadapter (): Sets the dialog box content to be a custom list item.

Setview (): Set dialog box content to Custom view.

Here are a few examples to illustrate the use of alertdialog.

1, the dialog box that displays the prompt message.

  

/**
  * Display prompt message dialog box
  * @author codingblock--
  @param  Context
  * @param title  dialog box Header
  * Param  Message dialog box prompts
  the content * @return
  /public
  Alertdialog.builder Simpledialog (final context, string title, String message} {
   Alertdialog.builder Builder = new Alertdialog.builder (context)
   . Settitle ( title)
   . SetIcon (R.drawable.ic_launcher). Setmessage (Message
   )
   . Setpositivebutton ("complete", NULL)
   . Setnegativebutton ("Cancel", null);
   return builder;
  }

The above code is to encapsulate a simple prompt dialog box into a method, call can omit duplicate code, directly pass Title,message parameters such as the dialog box with set Icon,title properties, also called Setpositivebutton () and the Setnegativebutton () method to add a button, because the method (Simpledialog ()) Here only provides the call, so there is no specific function to implement the button, you can override the two methods in the actual call to achieve the specific functionality.

The invocation is as follows, and the dialog boxes for several other ways are basically the same as the way this method is invoked, and the following are no longer given.

public void Onclicksimple (View v) {
   builder = new Dialog (). Simpledialog (This, Simple dialog box, dialog box content);
   Builder.setpositivebutton (OK), new Onclicklistener () {
    @Override public
    void OnClick (Dialoginterface arg, int arg) {
     //OK
    }
   }
   . Setnegativebutton ("Cancel", new Onclicklistener () {
    @Override public
    Void OnClick (dialoginterface arg, int arg) {
     //Cancel
    }
   });
   Builder.create (). Show ();
  

In addition, Alertdialog.builder provides a Setneutralbutton () method to add a decorative button. So the Android conversation can generate a three-button dialog box altogether.

2. Simple List Item dialog box

   

/**
  * Simple List Entry dialog box
  * @author codingblock--
  @param  Context
  * @param title  dialog box Header
  * @param Items  Dialog list items charsequence type array, can also be changed to other types as needed
  * @return * * * Public
  Alertdialog.builder Simplelistdialog (Final context, String title, final charsequence[] items) {
   Alertdialog.builder Builder = New Alertdialog.builder (context).
   Settitle (title).
   SetIcon (R.drawable.ic_launcher)
   . Setitems ( Items, new Onclicklistener () {
    
    @Override public
    void OnClick (dialoginterface dialog, int which) {
     Toast.maketext (Context, "You have selected:" + Items[which], toast.length_short). Show ();
    }
   return builder;
  }

The code above sets more than one list item for the dialog box by calling the Setitems () method, where the first argument of Setitems can be charsequence and int types.

3. Radio List Items dialog box

  

/**
  * Radio List Items dialog box
  * @author codingblock--
  @param  Context
  * @param title  dialog box Header
  * @param Items  dialog box list items charsequence type array
  * @return
  /Public Alertdialog.builder Simplechoicedialog (final Context context, String title, final charsequence[] items {
   Alertdialog.builder Builder = new Alertdialog.builder ( Context)
   . Settitle (title).
   SetIcon (R.drawable.ic_launcher)
   //The second parameter is the default check, which indicates that the first item is selected by default
   . Setsinglechoiceitems (items, new Onclicklistener () {
    @Override public
    void OnClick (Dialoginterface dialog, int which) {
     Toast.maketext (context, "you selected:" + Items[which], toast.length_short). Show ();
   return builder;
  }

The above code creates a dialog box with a radio list by calling the Setsinglechoiceitems () method. Calls to the Setsinglechoiceitems () method can either pass in an array as a parameter or pass in a cursor (equivalent to a database query result set) as an argument, or a listadapter as a parameter. In addition, if you pass in ListAdapter as a parameter, the ListAdapter provides multiple list item components.

4. Multi-selection List dialog box

  

 /**
  * Multi-select list Items dialog box
  * @author codingblock--
  @param  Context
  * @param title   dialog box Header
  * @param Items  Dialog list items charsequence type array
  * @param checked  dialog box initial selected state Boolean type array
  * @return
  * Public Alertdialog.builder Multichoicedialog (final context, String title, final charsequence[] items, final Boole An[] Checked {
   Alertdialog.builder Builder = new Alertdialog.builder (context)
   . Settitle (title).
   SetIcon (R.drawable.ic_launcher)
   //The second parameter is the default check, and is a Boolean array
   . Setmultichoiceitems (items, checked, null )
   . Setpositivebutton ("Done", null)
   . Setnegativebutton ("Cancel", null);
   return builder;
  }

The above code creates a dialog box for a multiple-selection list by calling the Setmultichoiceitems () method. You can either pass in the array as a parameter or pass in the cursor as a parameter when calling Setmultichoiceitems (). When you need to be aware, when you call the Setmultichoiceitems () method to add a multiple-selection list, you also need to pass in a boolean[] parameter that has two effects: ① the list items that are selected when the setting is initialized. ② parameters of this boolean[] type can also be used to dynamically get the selected state of a multiple-selection list item.

5, Custom list Items dialog box

  

 /**
  * Custom list Item dialog box
  * @author codingblock--
  @param  Context
  * @param title  dialog box Header
  * @param The Items  dialog box list item string array, but also more needs to be changed to other types
  * @return *
  /public
  Alertdialog.builder Customlistdialog ( Final context, String title, string[] items} {
   Alertdialog.builder Builder = new Alertdialog.builder (context )
   . Settitle (title).
   SetIcon (R.drawable.ic_launcher)
   . Setadapter (New arrayadapter<string> ( Context, R.layout.array_item, R.id.tv_item, items), null
   . Setpositivebutton ("complete", null).
   Setnegativebutton ("Cancel", null);
   return builder;
  }

The above code sets the contents of the dialog box by Setadapter (), which requires passing in a adapter parameter, so that the drawing of multiple components can be achieved by adapter. The layout file Array_item.xml code called in the Setadapter method is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
  xmlns:tools= "http:// Schemas.android.com/tools "
  android:id=" @+id/container "
  android:layout_width=" Match_parent "
  Android : layout_height= "match_parent"
  android:gravity= "center"
  android:padding= "DP"
  android:orientation= " Horizontal ">
  <imageview
   android:id=" @+id/iv_img "
   android:layout_width=" DP "
   android: Layout_height= "DP"
   android:src= "@drawable/ic_launcher"/>
  <textview android:id=
   "@+id/tv_ Item "
   android:layout_width=" fill_parent "
   android:layout_height=" wrap_content "
   android:layout_ Margin= "DP"
   android:gravity= "center"
   android:text= "list items"/>
 </LinearLayout>

In fact, not only can the Setadapter () method accept adapter as a parameter, the Setsinglechoice () method can also accept adapter parameters, that is, the Setsinglechoice () method can also implement the Custom List item dialog box.

6, Customize the View dialog box

  

/**
  * Custom View dialog box
  * @author codingblock--
  @param  Context
  * @param title  Dialog
  Public
  Alertdialog.builder Customeviewdialog (final context, String title) {
   LinearLayout logindialog = (LinearLayout) Layoutinflater.from (context). Inflate (r.layout.login_dialog, null);
   Alertdialog.builder Builder = new Alertdialog.builder (context)
   . Settitle (title)
   . SetIcon (r.drawable.ic_ Launcher)
   . Setview (logindialog).
   Setpositivebutton ("complete", null)
   . Setnegativebutton ("Cancel", null);
   return builder;
  }

The above code invokes the custom layout file display interface through the Setview () method. The code first shows the loaded Login_dialog.xml file, and returns the corresponding view of the file, and then the program calls the Setview () method to display view.

The Login_dialog.xml file code is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:id=" @+id/container "android:layout_width=" Match_parent "android:layout_height=" "Match_parent" android:padding= "DP" android:orientation= "vertical" > <linearlayout android:layout_width= "fi Ll_parent "android:layout_height=" wrap_content "android:orientation=" horizontal "> <textview android:id = "@+id/tv_name" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Username:"/&
   Gt <edittext android:id= "@+id/et_name" android:layout_width= match_parent "android:layout_height=" Wrap_conten T "android:focusable=" true "android:hint=" input name "/> </LinearLayout> <linearlayout Android : layout_width= "fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <TextVie W android:id= "@+id/tv_pwd" Android: layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Password:"/> <EditText and Roid:id= "@+id/et_pwd" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:hint= "I Nput password "/> </LinearLayout> </LinearLayout>

The above describes the implementation of six different dialog boxes, I hope to help you.

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.