Design your own prompt Dialog for Android Custom Controls

Source: Internet
Author: User

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

 

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.