Message prompt mechanism, used to convey information to users, prompt or warn users about their behavior. Common methods are setTitle, setMessage, create, and show. This article is mainly about the use of simple dialog box, how to customize dialog box, how to use PopupWindow for PopupWindow, and how to add animation effect to PopupWindow.

<Button android:id="@+id/ normal_dialog_bTN "Android :text=" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/diy_dialog_btn" Android :text=" custom dialog "Android :onClick="myClick" Android :layout_width="match_parent" android:layout_height="wrap_content"/>Copy the code

In fact, the normal dialog box is relatively simple:

public void myClick(View view) { switch (view.getId()){ case R.id.normal_dialog_btn: // AlertDialog AlertDialog.Builder builder = new AlertDialog.Builder(this); Builder. SetTitle (" tip "); Builder.setmessage (" Are you sure you want to exit the program?" ); Builder. SetPositiveButton (" sure, "(dialog, which) - > finish ()); Builder. SetNegativeButton (" cancel ", null); builder.show(); break; case R.id.diy_dialog_btn: break; }}Copy the code

Dialogs can also be created using custom methods:

Private void showNormalDialog(){AlertDialog AlertDialog = new Alertdialog.builder (this).create(); AlertDialog. SetTitle (" tip "); Alertdialog.setmessage (" Are you sure you want to exit the program?" ); SetButton (DialogInterface.BUTTON_POSITIVE, "Confirm ", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); }}); alertDialog.show(); }Copy the code

Steps: Set layout > Set Style > Custom Dialog > Show Dialog

Here is a lovely custom dialog:

Start with a new diy_dialog.xml layout file that describes the layout of our dialog:

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:gravity="center_horizontal" android:background="@mipmap/dialog_bg" android:padding="20dp" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" Android :layout_height="wrap_content" Android :text=" Are you sure you want to exit? android:textColor="#ff0000" android:textSize="35dp" android:layout_marginTop="260dp" android:textStyle="bold" /> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_marginTop="20dp" android:layout_height="wrap_content"> <Button android:id="@+id/no_btn" android:layout_width="120dp" android:layout_height="50dp" android:background="@mipmap/no_btn"/> <Button android:id="@+id/yes_btn" android:layout_width="120dp" android:layout_marginStart="20dp" android:layout_height="50dp" android:background="@mipmap/yes_btn"/> </LinearLayout> </LinearLayout>Copy the code

To remove the title and default background color from the original Dialog, define a style and call it DiyDialog:

<style > <! - the background color and transparent degree - > < item > @ android: color/transparent < item > <! <item >true</item> </style>Copy the code

DiyDialog > DiyDialog > DiyDialog > DiyDialog

public class DiyDialog extends Dialog { public DiyDialog(@NonNull Context context, int themeResId) { super(context, themeResId); // Set the layout for the dialog setContentView(r.layout.diy_dialog); // button to add click event findViewById(R.id.no_btn).setonClickListener ((e)->{this.dismiss(); }); findViewById(R.id.yes_btn).setOnClickListener((e)->{ System.exit(0); }); }}Copy the code

The constructor with themeResId was chosen above in order to be able to use a custom style.

When constructing a DiyDialog, note that you need to pass the defined style resource ID to it:

DiyDialog diyDialog = new DiyDialog(this, R.style.DiyDialog);
diyDialog.show();
Copy the code

PopUpWindow can be used in many ways, because it can pop up anywhere, which is difficult to do otherwise. As you can see from Google’s development documentation, it is a Window that pops up on top of other controls. PopupWindow:

Set the background, register the event listener, and add an animation. 3. Display PopupWindow

First button click event is written as showPopupWindow(View View), click the button PopupWindow.

Let’s start with the layout file popup_window.xml, which is basically three buttons

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:padding="2dp" android:background="#00ffff" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/chose_btn" android:layout_width="60dp" android:layout_height="30dp" android:gravity="center" android:textColor="#ffffff" android:background="#000000" Android :text=" select "/> <TextView Android :id="@+id/ chose_all_bTN "Android :layout_width="60dp" Android :layout_height="30dp" Android :textColor="# FFFFFF "Android :gravity="center" Android :background="#000000" Android :text=" all "/> <TextView android:id="@+id/copy_btn" android:layout_width="60dp" android:layout_height="30dp" android:textColor="#ffffff" Android :gravity="center" Android :background="#000000" Android :text=" duplicate "/> </LinearLayout>Copy the code

Then instantiate and display PopupWindow:

Private void showPopupWindow(View v) {// Instantiate View View = LayoutInflater. From (this).inflate(r.layout.popup_window, null); PopupWindow window = new PopupWindow(view, 360, 65, true); / / set the background to TRANSPARENT window. SetBackgroundDrawable (new ColorDrawable (Color. TRANSPARENT)); / / set can respond to external window. Click on the event setOutsideTouchable (true); // Set itself to respond to the click event window.settouchable (true); // Set animation (1. Create animation resource 2. Create a style application animation resource 3. Window. setAnimationStyle(R.style.translate_anim); // window. ShowAsDropDown (v); SetOnClickListener ((e)->{toasts. MakeText (mainActivity. this, "select ", Toast.LENGTH_SHORT).show(); // Don't forget to close PopupWindow window.dismiss(); }); View.findviewbyid (R.iC.hose_all_bTN).setonClickListener ((e)->{toasts. MakeText (mainActivity. this, "all ", Toast.LENGTH_SHORT).show(); window.dismiss(); }); View.findviewbyid (r.iC.opy_btn).setonClickListener ((e)->{toast.makeText (mainActivity. this, "Copy ", Toast.LENGTH_SHORT).show(); window.dismiss(); }); }Copy the code

How do you set up the animation? Create a style application animation resource. 3. Set the animation style of the current popover

Create anim directory under res directory:

Create a new animation resource file that contains only pan animations translate.xml

<? The XML version = "1.0" encoding = "utf-8"? > <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="200" android:toYDelta="0" > </translate> </set>Copy the code

Create a style application for the animation resource:

<! <style > <item >@anim/translate</item> </style>Copy the code

Then use it in code:

// Set animation (1. Create animation resource 2. Create a style application animation resource 3. Window. setAnimationStyle(R.style.translate_anim);Copy the code