android Dialog Custom example detailed _android

Source: Internet
Author: User
Tags xmlns
I have been working for more than one months. There are a lot of things about Android that have a new understanding or real mastery. In order to let more like me small white less detours, so I will insist that I encountered in the work of some of the more impressive knowledge points to integrate out to everyone (the order is based on my work to the present time to produce, in fact, also want to give themselves a record it.) Record your way along and the road ahead,
At least I want to find all the records I've ever had.
The first requirement: A simple custom Dialog
Requirements: Create a dialog that dialog has the following features
1. There is a window to display the article
2. According to the needs of the show
1) Click Agree (Disagree), trigger the corresponding event (the agreed event will pop up a toast, do not agree will close the program)
2) Click Close, Close dialog
1. Set a button in the Main.xml file and click to Eject dialog
Copy Code code 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, the layout set up the use of relative layout set three buttons, of which two appear together (agree and disagree), the other one appears (off) By default are two occurrences
Copy Code code as follows:

Private TextView btnclose = null;
Private TextView btnagree = null;
Private TextView btndisagree = null;
Private WebView Mwebview = null;
Public Mydialog {
Super (context, r.style.item_tnc_dialog);
Setcancelable (FALSE); Prevent response of 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 True, there are two options for agreeing and not agreeing, whereas turning off the option.
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.activity's code is not written. Write your custom dialog code directly
Copy Code code as follows:

Private TextView btnclose = null;
Private TextView btnagree = null;
Private TextView btndisagree = null;
Private WebView Mwebview = null;
Public Mydialog {
Super (context, r.style.item_tnc_dialog);
Setcancelable (FALSE); Prevent response of 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 True, there are two options for agreeing and not agreeing, whereas turning off the option.
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);
}

The effect chart is as follows:
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.