Android custom dialog instance

Source: Internet
Author: User

I have been working for more than a month. I have a new understanding or a real grasp of many Android things. In order to make more small and white people like me take a detour, I will insist on meeting
I have integrated some of my impressive knowledge points for you (the order is based on the time I have been working till now. In fact, I also want to give myself a record. Record your path and future path,
At least I want to find all the records I used to have ).

First requirement: simple custom Dialog
Requirement: Create a dialog with the following functions:
1. A window is displayed.Article
2. display as needed
1) Click agree (disagree) to trigger the corresponding event (A toast will pop up for the event of consent. Otherwise, the toast will be closed.Program)
2) Click Close to close the Dialog

1. Set a button in the main. xml file and click it to bring up the Dialog

View code

 
  
    xmlns: tools  = "http://schemas.android.com/tools" 
    Android: layout_width  = "match_parent" 
    Android: layout_height  =" match_parent "> 
    button Android: layout_width  = "100dp" 
    Android: layout_height  = "50dp" 
    Android: layout_centerhorizontal  =" true "
    Android: layout_centervertical  = "true" 
    Android: text  = "@ string/hello_world" 
    Android: ID  = "@ + ID/BTN" 
    Android: background  = "@ drawable/sl_btn_red"/> 
   

2. create the content layout of the dialog. Three buttons are set in the layout to use the relative layout. Two buttons appear together (agree and disagree), and the other one appear separately (close) the default value is two.

View code

 Private Textview btnclose = Null  ;  Private Textview btnagree = Null  ;  Private Textview btndisagree = Null  ;  Private Webview mwebview = Null ;  Public  Mydialog (context ){  Super  (Context, R. style. item_tnc_dialog); setcancelable (  False ); //  Block the response of the Return key  Setcontentview (R. layout. dialog_view); getwindow (). setlayout (layoutparams. match_parent, layoutparams. match_parent); setupview ();}  Private   Void Setupview () {mwebview = (Webview) findviewbyid (R. Id. item_tnc_dialog_webview); btnclose = (Textview) findviewbyid (R. Id. item_tnc_dialog_close); btnagree = (Textview) findviewbyid (R. Id. item_tnc_dialog_agree); btndisagree = (Textview) findviewbyid (R. Id. item_tnc_dialog_disagree); btnclose. setonclicklistener (  New  View. onclicklistener (){  Public   Void Onclick (view v) {dismiss () ;}}); btndisagree. setonclicklistener (  New  View. onclicklistener () {@ override  Public   Void  Onclick (view v) {dismiss (); system. Exit ( 0 ) ;}}); Btnagree. setonclicklistener (  New  View. onclicklistener () {@ override Public   Void  Onclick (view v) {toast. maketext (getcontext (), "You selected agree" , Toast. length_short). Show (); Dismiss () ;}}); showdialog ();}  /**  Load webview content  */          Public   Void  Showdialog () {string localhtml = "File: // android_asset/los.html";  If (Mwebview! = Null  ) {Mwebview. getsettings (). setdefaulttextencodingname ( "UTF-8" ); Mwebview. loadurl (localhtml);} buttonsdisplaytwo (  False  );}  /**  When the value is true, two options are "agree" and "disagree". Otherwise, the option is disabled.  */          Private   Void Buttonsdisplaytwo (Boolean  Two) {btnagree. setvisibility (two ? View. Visible: view. Gone); btndisagree. setvisibility (two ? View. Visible: view. Gone); btnclose. setvisibility (two ? View. Gone: view. Visible );} 

3. Main. ActivityCodeYou can stop writing .. Write custom dialog Code directly

View code

 Private Textview btnclose = Null  ;  Private Textview btnagree = Null  ;  Private Textview btndisagree = Null  ;  Private Webview mwebview = Null  ;  Public  Mydialog (context ){  Super  (Context, R. style. item_tnc_dialog); setcancelable (  False );//  Block the response of the Return key  Setcontentview (R. layout. dialog_view); getwindow (). setlayout (layoutparams. match_parent, layoutparams. match_parent); setupview ();}  Private   Void  Setupview () {mwebview = (Webview) findviewbyid (R. Id. item_tnc_dialog_webview); btnclose = (Textview) findviewbyid (R. Id. item_tnc_dialog_close); btnagree =(Textview) findviewbyid (R. Id. item_tnc_dialog_agree); btndisagree = (Textview) findviewbyid (R. Id. item_tnc_dialog_disagree); btnclose. setonclicklistener (  New  View. onclicklistener (){  Public   Void  Onclick (view v) {dismiss () ;}}); btndisagree. setonclicklistener (  New  View. onclicklistener () {@ override Public   Void  Onclick (view v) {dismiss (); system. Exit ( 0 ) ;}}); Btnagree. setonclicklistener (  New  View. onclicklistener () {@ override  Public   Void  Onclick (view v) {toast. maketext (getcontext (), "You selected agree", Toast. length_short). Show (); Dismiss () ;}}); showdialog ();}  /**  Load webview content  */          Public   Void  Showdialog () {string localhtml = "File: // android_asset/los.html" ;  If (Mwebview! = Null ) {Mwebview. getsettings (). setdefaulttextencodingname ( "UTF-8" ); Mwebview. loadurl (localhtml);} buttonsdisplaytwo (  False  );}  /**  When the value is true, two options are "agree" and "disagree". Otherwise, the option is disabled.  */          Private   Void Buttonsdisplaytwo ( Boolean  Two) {btnagree. setvisibility (two ?View. Visible: view. Gone); btndisagree. setvisibility (two ? View. Visible: view. Gone); btnclose. setvisibility (two ? View. Gone: view. Visible );} 

 

As follows:

 


Mydialog.rar

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.