Beginner Android 2016 (11) -- non-blocking dialog box AlertDialog

Source: Internet
Author: User

Beginner Android 2016 (11) -- non-blocking dialog box AlertDialog

After writing for so long and reading so many controls, it seems that they are static and there is no interaction at all. This time, you need to get a dialog box to be active.

Continue to use the code in the previous chapter to write it below.

First look at the figure

In the previous chapter, I bound the dialog box to the first icon.

Click here to see the following:

Let's take a look at the code.

Package com. fable. helloworld; import android. app. activity; import android. app. alertDialog; import android. content. context; import android. content. dialogInterface; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. gridView; import android. widget. simpleAdapter; import java. util. *; public class HelloWorldActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_hello_world); // you can specify the primary layout file: GridView (gridview) findViewById (R. id. gridview); // create the data source ArrayList
 
  
> Images = new ArrayList
  
   
> (); For (int I = 1; I <10; I ++) {HashMap
   
    
Map = new HashMap
    
     
(); Map. put ("ItemImage", R. drawable. ic_launcher); // Add the ID, identifier, and value map of the image resource. put ("ItemText", "app" + String. valueOf (I); // ItemText, identifier, value images by serial number. add (map);} // import data to the adapter and convert it to the data SimpleAdapter simpleAdapter = new SimpleAdapter (this, // The context is the current Activity images, // data source R. layout. my_list_item, // XML implementation of each item Layout new String [] {"ItemImage", "ItemText "}, // The subitem new int [] {R. id. itemImage, R. id. itemText}); // an ImageView in the XML file of ImageItem, two TextView IDs // Add and display the gridview. setAdapter (simpleAdapter); // Add a message to process the gridview. setOnItemClickListener (new ItemClickListener ();} // when AdapterView is clicked (touch screen or keyboard), the returned Item clicks the event class ItemClickListener implements OnItemClickListener {public void onItemClick (AdapterView)
     Arg0, // parent View arg1, // current View int arg2, // click long arg3 // id) {HashMap
     
      
Item = (HashMap
      
        ) Arg0.getItemAtPosition (arg2); // obtain the clicked item // setTitle (String) item. get ("ItemText"); // This is just to change the title, showDialog (HelloWorldActivity. this, (String) item. get ("ItemText");} private void showDialog (Context context, String itemStr) {if (itemStr. equals ("app1") {// click the first icon. // The constructor of AlertAialog is protected. You can only use the Builder function to construct a new object AlertDialog. builder builder = new AlertDialog. builder (context); builder. setIcon (R. Drawable. ic_launcher); // set the icon builder. setTitle ("I am the title"); // set the title builder. setMessage ("here is the content !!! "); // Set the content builder. setPositiveButton ("Button1", // confirmation button new DialogInterface. onClickListener () {// For convenience, do not explicitly declare a class public void onClick (DialogInterface dialog, int whichButton) {setTitle ("Button1 clicked on the dialog box") ;}}); builder. setNeutralButton ("Button2", // neutral button new DialogInterface. onClickListener () {public void onClick (DialogInterface Diener, int whichButton) {setTitle ("Click Button2") ;}}); builder. setNegativeButton ("Button3", // new DialogInterface. onClickListener () {public void onClick (DialogInterface Diener, int whichButton) {setTitle ("Click Button3") ;}}); builder. show (); // explicit dialog box }}}}
      
     
    
   
  
 

 

 

You can see that the dialog box has three buttons: setPositiveButton, setNeutralButton, and setNegativeButton.

Literally, it means confirmation, being neutral, and denying the three meanings. However, there is no rule for the events bound to the three buttons. You can write any event or perform any operation. Of course, in order to facilitate reading, we should try our best to conform to the original meaning.

SetTitle is the function of the Activity. It sets the title.

Click the button to see it?

 

The following is an xml layout file, which has not been changed as in the previous chapter, but is pasted out:

Activity_hello_world.xml

   
   
 

My_list_item.xml
   
            
              
             
             
    
   

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.