Analysis dialog box AlertDialog

Source: Internet
Author: User

Analysis dialog box AlertDialog

Go directly to the code.


Simple layout. Three buttons:

         
          
          
          
 


Main Function

Package com. example. ershisi; import android. support. v7.app. actionBarActivity; import android. support. v7.app. actionBar; import android. support. v4.app. fragment; import android. test. isolatedContext; import android. app. alertDialog; import android. content. dialogInterface; import android. content. dialogInterface. onMultiChoiceClickListener; import android. OS. bundle; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. widget. button; import android. widget. toast; import android. OS. build; public class MainActivity extends ActionBarActivity {AlertDialog. builder build = null; AlertDialog alert = null; boolean checkItems []; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button liebiao = (Button) findViewById (R. id. liebiao); Button danxuan = (Button) findViewById (R. id. danxuan); Button duoxuan = (Button) findViewById (R. id. duoxuan);/* alert = new AlertDialog. builder (MainActivity. this ). create (); alert. setIcon (R. drawable. icon); alert. setMessage ("this is a neutral button"); alert. setTitle ("button learning"); alert. setButton (DialogInterface. BUTTON_NEGATIVE, "OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub}); alert. setButton (DialogInterface. BUTTON_POSITIVE, "cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub}); alert. show (); */liebiao. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubfinal String [] items = {"apple", "orange ", "banana", "strawberry"}; build = new AlertDialog. builder (MainActivity. this); build. setIcon (R. drawable. icon); build. setTitle ("select the fruit you like"); build. setItems (items, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubToast. makeText (getApplicationContext (), "you choose" + items [which], Toast. LENGTH_LONG ). show () ;}}); alert = build. create (); alert. show () ;}}); danxuan. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubfinal String [] fruits = {"apple", "orange ", "banana", "strawberry"}; build = new AlertDialog. builder (MainActivity. this); build. setIcon (R. drawable. icon); build. setTitle ("only one favorite fruit doll can be selected"); build. setSingleChoiceItems (fruits, 0, new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubToast. makeText (getApplicationContext (), "you choose" + fruits [which], Toast. LENGTH_LONG ). show () ;}}); alert = build. create (); alert. show () ;}}); duoxuan. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubcheckItems = new boolean [] {false, false, false }; final String [] menu = {"noodle", "Spicy Chicken", "pancake"}; build = new AlertDialog. builder (MainActivity. this); build. setIcon (R. drawable. icon); build. setMultiChoiceItems (menu, checkItems, new OnMultiChoiceClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubcheckItems [which] = isChecked ;}}); build. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubString res = ""; for (int I = 0; I
 
  


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PGJyPgo8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20150125/2015012509373847.jpg" alt = "\">

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.