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, Boolean cancelable,
Oncancellistener cancellistener ){
Super (context, cancelable, cancellistener );
}
2. Public mycameradialog (context, int theme ){
Super (context, theme );
}
3. Public mycameradialog (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 ){
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, int theme) {super (context, theme); this. mcontext = context;} public mycameradialog (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 = 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 = new intent (intent. action_pick, android. provider. mediastore. images. media. external_content_uri); (activity) mcontext ). startactivityforresult (intent, 1 );}});}}
About custom Dialog