Detailed description of android dialog custom instances

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 people like me less detours, therefore, I will insist on integrating some of my most impressive knowledge points at work (the order is based on my work time, 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. There is a window to show the article
2. display as needed
1) Click agree (disagree) to trigger the corresponding event (A Toast will pop up for the event of consent, and the program will be closed if it does not agree)
2) Click Close to close the dialog
1. Set a button in the main. xml file and click it to bring up the dialog Copy codeThe Code is as follows: <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
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"/>
</RelativeLayout>

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.Copy codeThe Code is as follows: private TextView btnClose = null;
Private TextView btnAgree = null;
Private TextView btnDisagree = null;
Private WebView mWebView = null;
Public MyDialog (Context 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 it is true, two options are "agree" and "disagree", and vice versa */
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. The Code of main. activity will not be written .. Write custom dialog Code directlyCopy codeThe Code is as follows: private TextView btnClose = null;
Private TextView btnAgree = null;
Private TextView btnDisagree = null;
Private WebView mWebView = null;
Public MyDialog (Context 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 it is true, two options are "agree" and "disagree", and vice versa */
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:

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.