When you need to design your own dialog box dialog and customize it according to your own requirements, such as the most common operation prompt, the operation is successful, and the corresponding prompt is displayed when the operation fails.
First look at the effect:
Steps:
1. inherit from the dialog class and define SEVERAL VARIABLES
Private Boolean Flag; // What images are displayed?
Private String MSG, title; // Title and content
Private Textview tvmsg; // Display message box in layout File
Private Imageview icon; // Show image button in layout File
Private Button close; // Close button in layout File
2. Implement the parent class Constructor
Public Mydialog (context, Boolean Flag, String title, string MSG)
{
Super (Context );
This . Flag = flag;
This . MSG = MSG;
This . Title = title;
}
3. Override the oncreate method of the parent class
View code
Protected Void Oncreate (bundle savedinstancestate)
{
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Dialog );
Tvmsg = (textview) findviewbyid (R. Id. msg );
Icon = (imageview) findviewbyid (R. Id. Icon );
Close = (button) findviewbyid (R. Id. OK );
If (FLAG)
Icon. setimageresource (R. drawable. OK );
Else Icon. setimageresource (R. drawable. Error );
Settitle (title );
Tvmsg. settext (MSG );
Setonclicklistener ();
}
4. Add a listener to the button
Private Void Setonclicklistener (){
Close. setonclicklistener ( New View. onclicklistener ()
{
@ Override
Public Void Onclick (view V)
{
Dismiss ();
}
});
}
Call method:
Mydialog dialog = new mydialog (this, false, "Information prompt", "Your operation is incorrect ");
Dialog. Show ();
Layout file. I will directly upload my files for you to download:/files/jaylong/dialog. xml