Use AlertDialog to create six kinds of dialog boxes

Source: Internet
Author: User

Use AlertDialog to create six kinds of dialog boxes

The dialog box generated by AlertDialog can be divided into four areas: icon area, title area, content area, and button area.

Structure

Use of the AlertDialog dialog box:

1,Create AlertDialog. Builder object
2. CallSetTitle () sets the title and setIcon sets the icon
3. Call related methods of the Builder objectSet content, AlertDialog provides the following settings for the specified dialog box:

SetMessage (); set the content of a simple text box
SetItems (); set the content of the simple list, array
SetSingleChoiceItems (); sets the content of the single-choice list. The content parameter can be an array of Cursor and ListAdapter.
SetMultiChoiceItems (); sets the content of multiple-choice list items. The content parameter can be an array, Cursor
SetAdapter (); set the content, the content is ListAdapter, commonly used BaseAdapter, SimpleAdapter, ArrayAdapter
SetView (); set the content. The parameter is a custom View.
4. CallSetPositiveButton () and setNegativeButton () set buttons and listeners
5. CallCreate () methodCreate an AlertDialog object and then callShow () methodShow dialog box

 

In short: Call the Builder object settings icon, title, content, button, in create (), show ()

Code Template:

 

New AlertDialog. builder (this) // set the dialog box title. setTitle ("simple dialog box") // set the icon. setIcon (R. drawable. tools) // set the content, which can be replaced. setMessage ("Test content in the dialog box \ n Second content "). setPositiveButton ("OK", listener ). setNegativeButton ("cancel", listener ). create (). show ();
Acitivity_main.xml

 

 

 
     
      
           
    
     
     
                     
       
      
     
     
     
    
   
  
 


MainActivity. java

 

 

 

Package com. hust. alertdialogtest; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. content. dialogInterface. onMultiChoiceClickListener; import android. OS. bundle; import android. view. menu; import android. view. menuItem; import android. view. view; import android. widget. arrayAdapter; import android. widget. tableLayout; import android. widget. textView;/* use of the AlertDialog dialog box: * The AlertDialog dialog box has four areas: icon area, title area, content area, and button area * 1. Create AlertDialog. builder object * 2: setTitle () of the Builder object, setIcon setting icon * 3, and setMessage () of the Builder object (); set the content of the simple text box * setItems (); set the content of the simple list, array * setSingleChoiceItems (); set the content of the single-choice list, the content parameter can be an array, Cursor, listAdapter * setMultiChoiceItems (); set the content of multiple-choice list items. The content parameter can be an array, Cursor * setAdapter (); set the content, the content is ListAdapter, commonly used BaseAdapter, SimpleAdapter, arrayAdapter * setView (); set content. The parameter is custom View * 4. Call setPositiveButton () and setNegativeButton () Setting buttons and listener * 5 of the Builder object, call the create () method of the Builder object to create the AlertDialog object, and then call the show () method of the AlertDialog object to display the dialog box. *** in short, call the Builder object setting icon, title, content, and button, in create (), show () **/public class MainActivity extends Activity {TextView show; String [] items = new String [] {"Hubei Province", "Fujian Province ", "Guizhou Province", "Sichuan Province"}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); show = (TextView) findViewById (R. id. textView1);}/* simple text dialog box: builder. setMessage () Setting content */public void simple (View v) {AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("simple dialog box"); builder. setIcon (R. drawable. tools); builder. setMessage ("this is a simple text dialog box \ n Second line content"); // Add a confirmation button for the builder object, but a function setPositiveButton (builder) is nested here ); // Add the cancel button to the builder object builder = setNegativeButton (builder); // create the dialog box object AlertDialog simpledialog = builder. create (); simpledialog. show ();}/* simple List dialog box (array dialog box): builder. setItems () Setting content */public void simpleList (View v) {AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("array List dialog box"); builder. setIcon (R. drawable. tools); builder. setItems (items, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubshow. setText ("You clicked" + items [which]) ;}}); setPositiveButton (builder); builder = setNegativeButton (builder); AlertDialog simplelistdialog = builder. create (); simplelistdialog. show ();}/* single-choice list item dialog box: builder. setSingleChoiceItems () content. The content parameter can be an array, Cursor, ListAdapter */public void singleChoice (View v) {AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("Single-choice list items dialog box"); builder. setIcon (R. drawable. tools); // set the radio list item. The second builder is selected by default. setSingleChoiceItems (items, 1, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubshow. setText ("You clicked" + items [which]) ;}}); setPositiveButton (builder); builder = setNegativeButton (builder); AlertDialog simplechoicedialog = builder. create (); simplechoicedialog. show ();}/*** multiple-choice list items dialog box: builder. setMultiChoiceItems () Setting content. The parameter can be an array. The Cursor database returns the result set ** AlertDialog. builder. setMultiChoiceItems (CharSequence [] items, boolean [] checkedItems, OnMultiChoiceClickListener listener) */public void multiChoice (View v) {AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("Multi-choice list items dialog box"); builder. setIcon (R. drawable. tools); // you can specify multiple options. The second option and the fourth item are selected by default. setMultiChoiceItems (items, new boolean [] {false, true, false, true}, new OnMultiChoiceClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stub // Add processing method}); setPositiveButton (builder); builder = setNegativeButton (builder); AlertDialog simplechoicedialog = builder. create (); simplechoicedialog. show ();}/* Custom Adapter dialog box: builder. setAdapter content. The content is ListAdapter, commonly used BaseAdapter, SimpleAdapter, ArrayAdapter */public void customList (View v) {AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("Custom Adapter dialog box"); builder. setIcon (R. drawable. tools); builder. setAdapter (new ArrayAdapter
 
  
(This, R. layout. array_item, items), new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubshow. setText ("You clicked" + items [which]) ;}}); // Add a confirmation button for the builder object, but a setPositiveButton (builder) function is nested here ); // Add the cancel button to the builder object builder = setNegativeButton (builder); // create a dialog box object AlertDialog adapterdialog = builder. create (); adapterdialog. show ();}/* Custom View dialog box: builder. setView () set View */public void customView (View v) {// get the xml layout file object TableLayout loginform = (TableLayout) getLayoutInflater (). inflate (R. layout. login, null); AlertDialog. builder builder = new AlertDialog. builder (this); builder. setTitle ("Custom View dialog box"); builder. setIcon (R. drawable. tools); // set the View component builder displayed in the dialog box. setView (loginform); // Add a confirmation button for the builder object. However, a function setPositiveButton (bui Lder); // Add the cancel button for the builder object builder = setNegativeButton (builder); // create a dialog box object AlertDialog viewdialog = builder. create (); viewdialog. show () ;}// the returned object is the original Builder object private AlertDialog. builder setPositiveButton (AlertDialog. builder builder) {// TODO Auto-generated method stub return builder. setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int Which) {// TODO Auto-generated method stubshow. setText ("click OK! ") ;}}) ;}// The returned object is the Builder object private AlertDialog. builder setNegativeButton (AlertDialog. builder builder) {// TODO Auto-generated method stub return builder. setNegativeButton ("cancel", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubshow. setText ("click the cancel button! ") ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Override public boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest. xml. int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}
 

 

 



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.