Getting started with Android 2016 (13)-blocking dialog box PopupWindow

Source: Internet
Author: User

Getting started with Android 2016 (13)-blocking dialog box PopupWindow

The non-blocking dialog box is described in the previous two chapters. Today we will talk about the blocking dialog box-PopupWindow

So what is blocking and what is non-blocking? What is the difference between PopupWindow and AlertDialog?

First, let's talk about AlertDialog. After the pop-up, the back is grayed out, and the background process is not blocked. If there is no special control, click the dimmed background and the pop-up box will disappear.

As for the PopupWindow, It is popped up, and there is no change in the background, and the process blocking the application is blocked. If the application has not exited, the application waits for a direct wait, and no response is returned after clicking it.

I don't know why I can't upload a graph now, so I won't upload it. It's actually the same as AlertDialog.

Continue to use the previous code to expand

Let's see how to implement it:

Added a popup_paipaixml

 

     
         
          
     
    
     
     
      
   
  
   
Declares a title, a dialog box, and two buttons.

Then let's look at the implementation code. If you have read AlertDialog, you can adjust it to the following code.

Package com. fable. helloworld; import android. app. activity; import android. app. alertDialog; import android. content. context; import android. content. dialogInterface; import android. content. res. resources; import android. graphics. color; import android. graphics. drawable. drawable; import android. OS. bundle; import android. view. gravity; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. button; import android. widget. editText; import android. widget. gridView; import android. widget. popupWindow; 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 ++) {String imageName = ""; switch (I) {case 1: imageName = "AlertDialog "; // normal AlertDialog break; case 2: imageName = "AlertDialog2"; // Layout-based AlertDialog break; case 3: imageName = "PopupWindow"; // the break of the blocking dialog box; default: imageName = "app" + String. valueOf (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", imageName); // perform ItemText, identifier, and 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, String itemStr = (String) item. get ("ItemText"); if (itemStr. equals ("AlertDialog") {showDialog (HelloWorldActivity. this, itemStr);} else if (itemStr. equals ("AlertDialog2") {showDialogLayout (HelloWorldActivity. this);} else if (itemStr. equals ("PopupWindow") {showPopupWindow (HelloWorldActivity. this, arg 1 );}} // ==================================== AlertDialog ================ ========================================================== private void showDialog (Context context, string itemStr) {// 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); // sets 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} // ================================== AlertDialog Based on Layout ======== ========================================================== private void showDialogLayout (Context context) {// LayoutInflater is used to dynamically load the Layout inflater = LayoutInflater of the Layout file. from (context); final View textEntryView = inflater. inflate (R. layout. dialog_layout, null); // dynamically loads the Layout file final EditText edtInput = (EditText) textEntryView. findViewById (R. id. edtInput); // after loading, you can find the final AlertDialog control. builder builder = new AlertDialog. builder (context); builder. setCancelable (false); builder. setIcon (R. drawable. ic_launcher); builder. setTitle ("Title"); builder. setView (textEntryView); builder. setPositiveButton ("OK", // you can see that the new DialogInterface is mixed. onClickListener () {public void onClick (DialogInterface dialog, int whichButton) {setTitle (edtInput. getText () ;}}); builder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {public void onClick (DialogInterface Diener, int whichButton) {setTitle ("") ;}}); builder. show ();} // ================================ PopupWindow ===================== ============================== private void showPopupWindow (Context context, view parent) {// LayoutInflater is used to dynamically load the Layout inflater = LayoutInflater of the Layout file. from (context); final View popupView = inflater. inflate (R. layout. popup_window, null); // dynamically load the Layout file final PopupWindow pWindow = new PopupWindow (popupView, 200,200, true); // you need to specify the width and height, otherwise, the final Button button = (Button) popupView cannot be displayed. findViewById (R. id. btnOK); // after loading, you can find the control button. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// set the content of the EditText edtUsername = (EditText) popupView text box. findViewById (R. id. data_edit); edtUsername. setText ("follow: Legend Road") ;}}); // The Cancel Button and its processing event Button btnCancel = (Button) popupView. findViewById (R. id. btnCancel); btnCancel. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {pWindow. dismiss (); // close}); // displays the pWindow in the popupWindow dialog box. showAtLocation (parent, Gravity. CENTER, 0, 0 );}}}
      
     
    
   
  
 

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.