About custom Dialog, about dialog

Source: Internet
Author: User

About custom Dialog, about dialog

Dialog is the legendary pop-up box. Some of the layout and functions provided by the system are relatively simple and generally cannot meet the development requirements. Therefore, we need to implement a custom Dialog by ourselves.

First, if you want to implement your own Dialog, We need to inherit from the Dialog. The Dialog constructor has three formats. We need at least one of the lifecycles to use them.

The constructor format is as follows:

1. public MyCameraDialog (Context context, boolean cancelable,

OnCancelListener cancelListener ){

Super (context, cancelable, cancelListener );

}

2. public MyCameraDialog (Context context, int theme ){

Super (context, theme );

}

3. public MyCameraDialog (Context context ){

Super (context );

}

Among the three constructors, we usually use the second one, that is, the constructor that requires context objects and styles. When using this constructor, We need to specify a style, for example, there is no title.

Of course, we can use the third constructor to pass in only one context object, and then set a default style that we specify. As shown below:

Public MyCameraDialog (Context context ){

This (context, R. style. MyStyle );

}

In this way, when we create a window, we can use our default display style, R. style. myStyle refers to a Dialog style set in the style file. Below are common settings.

<Style name = "CustomDialog" parent = "android: style/Theme. Dialog"> <! -- Background color and transparency --> <item name = "android: windowBackground"> @ android: color/transparent </item> <! -- Whether the title exists --> <item name = "android: windowNoTitle"> true </item> <! -- Whether to float above activity --> <item name = "android: windowIsFloating"> true </item> <! -- Whether fuzzy --> <item name = "android: backgroundDimEnabled"> false </item> <item name = "android: windowFrame"> @ null </item> </style>

After setting the style, we need to use our custom layout to set the display screen.

In Dialog, if we want to use our own custom layout, it is very simple. We only need to rewrite the onCreate method, just like we are writing an Activity layout, in onCreate, we can use

SetContentView (R. layout. dialog); To set the layout to be filled, and then bind the control events in the layout.

After setting these items, we can use new MyDialog (context). show () to display our custom Dialog.

The following is an example of a custom Dialog that is used to select a photo or take a photo to obtain a photo. For details, refer

/*** Custom photo source dialog ** @ author ZhaoKaiQiang ** Time: July 15, March 12, 2014 */public class MyCameraDialog extends Dialog {private TextView TV _my_camera_take_photo; private TextView TV _my_camera_from_album; private Context mContext; private static int mTheme = R. style. myDialog;/*** custom Dialog ** @ param context * @ param activity * The Activity where the Dialog is located, click "OK" and finish the text on the Activity * @ param title * text content * @ param btnString * button * @ param theme * style */public MyCameraDialog (Context context Context, int theme) {super (context, theme); this. mContext = context;} public MyCameraDialog (Context context) {super (context, mTheme); this. mContext = context;} @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. dialog_select_photo); TV _my_camera_take_photo = (TextView) findViewById (R. id. TV _my_camera_take_photo); TV _my_camera_from_album = (TextView) findViewById (R. id. TV _my_camera_from_album); TV _my_camera_take_photo.setOnClickListener (new android. view. view. onClickListener () {@ Overridepublic void onClick (View v) {MyCameraDialog. this. dismiss (); // select Intent intent = new Intent ("android. media. action. IMAGE_CAPTURE "); (Activity) mContext ). startActivityForResult (intent, 0) ;}}); TV _my_camera_from_album.setOnClickListener (new android. view. view. onClickListener () {@ Overridepublic void onClick (View v) {// select MyCameraDialog from the album. this. dismiss (); Intent intent = new Intent (Intent. ACTION_PICK, android. provider. mediaStore. images. media. EXTERNAL_CONTENT_URI); (Activity) mContext ). startActivityForResult (intent, 1 );}});}}



Optimize the pop-up speed of custom Dialog

Do you re-create the Dialog every time? It will take a little longer. Android has an OnCreateDialog and OnPreparedDialog optimization method. That is to say, you only need to create one in OnCreateDialog and call OnPreparedDialog for updates in other cases.
 
How to display the custom MFC dialog Interface

There is no function after your menu preset ......
In addition, if you want to display the dialog box, you must first instantiate it,
We recommend that you first look at the basic teaching of VC ++,
Sun Xin's video or in-depth introduction to MFC

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.