Android PopupWindow and Activity pop-up window implementation method

Source: Internet
Author: User

I want to cook a dish. Currently, we have only seen two pop-up box implementation methods. The first is the most common PopupWindow, and the second is the Activity method that we have seen a few days ago. I feel so domineering. Unexpectedly, the activity can also be used as a pseudo window.
Paste the most common method first, mainly about the activity method.
I. Pop Up The PopupWindow
Copy codeThe Code is as follows :/**
* The menu is displayed.
*/
Public void menu_press (){
If (! Menu_display ){
// Obtain the LayoutInflater instance
Inflater = (LayoutInflater) this. getSystemService (LAYOUT_INFLATER_SERVICE );
// Here, the main layout is added in inflate. In the past, this. setContentView () was used directly, right? Haha
// This method returns a View object, which is the root of the layout.
Layout = inflater. inflate (R. layout. main_menu, null );
// Now we have to consider how to add my layout to the PopupWindow ??? Simple
MenuWindow = new PopupWindow (layout, LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT); // The last two parameters are width and height.
// MenuWindow. showAsDropDown (layout); // set the pop-up effect
// MenuWindow. showAsDropDown (null, 0, layout. getHeight ());
// Set the following information. When you click another area to hide it, you must configure it before show.
MenuWindow. setFocusable (true );
MenuWindow. setOutsideTouchable (true );
MenuWindow. update ();
MenuWindow. setBackgroundDrawable (new BitmapDrawable ());
MClose = (LinearLayout) layout. findViewById (R. id. menu_close );
MenuWindow. showAtLocation (this. findViewById (R. id. schoolmain), Gravity. BOTTOM | Gravity. CENTER_HORIZONTAL,); // you can specify the position of layout in PopupWindow.
// How do I obtain the controls in main? It is also very simple
MMainbtn = (LinearLayout) layout. findViewById (R. id. menu_main_btn );
MHistorybtn = (LinearLayout) layout. findViewById (R. id. menu_history_btn );
MHelpbtn = (LinearLayout) layout. findViewById (R. id. menu_help_btn );
// Register the click event for each Layout...
// For example, when a MenuItem is clicked, its background color changes
// Prepare some background images or colors in advance
MMainbtn. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View arg0 ){
MywebView. loadUrl (URL );
MenuWindow. dismiss (); // closes the Menu after responding to the click event.
}
});
MHelpbtn. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View arg0 ){
MywebView. loadUrl (URL );
MenuWindow. dismiss (); // responds to the Click Event
}
});
MHistorybtn. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View arg0 ){
MywebView. loadUrl (URL );
MenuWindow. dismiss (); // responds to the Click Event
}
});
Menu_display = true;
} Else {
// Hide it if the current status is displayed
MenuWindow. dismiss ();
Menu_display = false;
}
}
Public void back_press (){
If (menu_display) {// if Menu is enabled, Disable Menu first
MenuWindow. dismiss ();
Menu_display = false;
}
Else {
Intent intent = new Intent ();
Intent. setClass (MainActivity. this, Exit. class );
StartActivity (intent );
}
}

This method is very simple. If you want to hide it by clicking somewhere else, you need to set four attributes in show as follows:
// Focus it
MPopupWindow. setFocusable (true );
// Set to allow click to disappear
MPopupWindow. setOutsideTouchable (true );
// Refresh status
MPopupWindow. update ();
// Click the back key and remove it from other locations. If this parameter is set, OnDismisslistener can be triggered, and other control changes can be set.
MPopupWindow. setBackgroundDrawable (new BitmapDrawable ());
2. Create a pop-up window for the Activity

I think this is very advanced. Forgive me for being offended. I only want to write the code of the ox here. The cool man wrote the anti-DDoS demo. I have attached a free CSDN download link.
First paste the java code -- Exit. java
Package cn. buaa. myweixin;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. LinearLayout;
Import android. widget. Toast;
Public class Exit extends Activity {
Private LinearLayout layout;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. exit_dialog );
Layout = (LinearLayout) findViewById (R. id. exit_layout );
Layout. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Toast. makeText (getApplicationContext (), "prompt: Click the window to close the window! ",
Toast. LENGTH_SHORT). show ();
}
});
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Finish ();
Return true;
}
Public void exitbutton1 (View v ){
This. finish ();
}
Public void exitbutton0 (View v ){
This. finish ();
MainWeixin. instance. finish (); // close the Activity of Main.
}
}
Attached layout file: exit_dialog.xmlCopy codeThe Code is as follows: View Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/exit_layout"
Android: layout_width = "280dp"
Android: layout_height = "wrap_content"
Android: gravity = "center_horizontal"
Android: orientation = "vertical"
Android: background = "@ drawable/confirm_dialog_bg2">
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "4dp"
Android: padding = "5dp"
Android: textColor = "#333"
Android: textSize = "20sp"
Android: text = "quit"/>
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "#333"
Android: layout_marginTop = "1dp"
Android: padding = "10dp"
Android: textSize = "16sp"
Android: gravity = "center_horizontal"
Android: text = "after exiting, you will not receive the new message \ n. Are you sure you want to quit? "/>
<LinearLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "33dp"
Android: layout_marginBottom = "8dp"
>
<Button
Android: id = "@ + id/exitBtn0"
Android: layout_width = "110dp"
Android: layout_height = "wrap_content"
Android: text = "yes"
Android: textSize = "16sp"
Android: textColor = "# fff"
Android: background = "@ drawable/btn_style_green"
Android: gravity = "center"
Android: onClick = "exitbutton0"
/>
<Button
Android: id = "@ + id/exitBtn1"
Android: layout_width = "110dp"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "10dp"
Android: text = "no"
Android: textSize = "16sp"
Android: textColor = "#333"
Android: background = "@ drawable/btn_style_white"
Android: gravity = "center"
Android: onClick = "exitbutton1"
/>
</LinearLayout>
</LinearLayout>

Analysis, the pop-up exit box is very simple, itself is the activity, in the main interface using startavti.pdf and other methods like ordinary activity to start.
The key is how to click somewhere else to exit and close the program.
Click other places to exit. You only need to listen to OnClickListener to exit all clicks (except for the button events specified in xml ).
And exit the main program. As long as the main program is set as a static object in the output program, public static MainWeixin instance = null can be called externally;
The above layout and activity do not display the activity form on the main interface, it looks like a pop-up box. Below is the pop-up boxStyles Configuration: Copy codeThe Code is as follows: <style name = "MyDialogStyle">
<Item name = "android: windowBackground"> @ android: color/transparent </item>
<Item name = "android: windowFrame"> @ null </item>
<Item name = "android: windowNoTitle"> true </item>
<Item name = "android: incluwisfloating"> true </item>
<Item name = "android: javaswistranslucent"> true </item>
<Item name = "android: windowContentOverlay"> @ null </item>
<Item name = "android: windowAnimationStyle"> @ android: style/Animation. Dialog </item>
<Item name = "android: backgroundDimEnabled"> true </item>
</Style>

Next we will analyze the meaning and focus of this layout:
Key 1: <item name = "android: windowBackground"> @ android: color/transparent </item> background color
Key 2: The windowFrame box of <item name = "android: windowFrame"> @ null </item> Dialog is none.
Key 4: <item name = "android: javaswisfloating"> true </item> whether the image is floating above the activity
Key 5: <item name = "android: javaswistranslucent"> true </item> whether the window is translucent-Yes (used with the first entry)
Key 6: <item name = "android: windowAnimationStyle"> @ android: style/Animation. Dialog </item>
Key 7: <item name = "android: backgroundDimEnabled"> true </item> whether to allow background blurring
Key 8: <item name = "android: windowContentOverlay"> @ null </item> if this parameter is not set, black borders may appear on the border.
The above code is the credit of the cool people. I used it for analysis and gave it to more friends who needed it.
I grew up step by step under the guidance of many cool people and thanked the cool people.

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.