How to implement Android custom dialog

Source: Internet
Author: User

Recently has been doing Java-related things, although have been looking at Android but feel a little to stay in theory, always so not, write more not necessarily know more, but want to know more on a
Set to write more, so today to write a bit Dialog about the things, is a sort of Dialog.

Alertdialog use not to say, casually Baidu can find, here to say, mainly is the custom of Dialog.

Then, let's start with the code:

public class Customdialog extends Alertdialog {

Protected Customdialog (Context context) {
Super (context);
}

Protected Customdialog (Context context, Boolean cancelable, Oncancellistener cancellistener) {
Super (context, cancelable, Cancellistener);
}

Protected Customdialog (context context, @StyleRes int themeresid) {
Super (context, THEMERESID);
}

public static class Builder {

Private Context Mcontext;
Private Customdialog Dialog;
Private View layout;
private int micon =-1;
Private String MText;
Private String mcontent;
Private String mpoibtn;
Private View.onclicklistener Mpoilistener;
Private String mnegbtn;
Private View.onclicklistener Mneglistener;
private int animatetype =-1;
public static final int low_disappear_animation = 1;
public static final int rotate_disappear_animation = 2;

Public Builder (Context context) {
Mcontext = context;
dialog = new Customdialog (context, R.style.theme_appcompat_dialog_alert);
Layout = View.inflate (mcontext, r.layout.custom_dialog1, NULL);

}

Public Builder settitleicon (int iconres) {
Micon = Iconres;
return this;
}

Public Builder settitle (charsequence text) {
MText = (String) text;
return this;
}

Public Builder setcontent (charsequence text) {
Mcontent = (String) text;
return this;
}

Public Builder Setpositivebutton (charsequence text, @NonNull View.onclicklistener
Clicklistener) {
MPOIBTN = (String) text;
Mpoilistener = Clicklistener;
return this;
}

Public Builder Setnegativebutton (charsequence text, @NonNull View.onclicklistener
Clicklistener) {
MNEGBTN = (String) text;
Mneglistener = Clicklistener;
return this;
}

Public Builder Create () {
ImageView Titleicon = (ImageView) Layout.findviewbyid (R.id.title_icon);
TextView Contentview = (TextView) Layout.findviewbyid (r.id.custom_content);
TextView Titleview = (TextView) Layout.findviewbyid (r.id.title);
TextView poibtn = (TextView) Layout.findviewbyid (r.id.custom_confirm);
TextView negbtn = (TextView) Layout.findviewbyid (r.id.custom_cancel);
Poibtn.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Dialog.dismiss ();
}
});
if (Micon! =-1) {
Titleicon.setimagebitmap (Bitmapfactory.decoderesource (Mcontext.getresources (),
Micon));
}

if (! Textutils.isempty (MText)) {
Titleview.settext (MText);
}

if (! Textutils.isempty (mcontent)) {
Contentview.settext (mcontent);
}

if (! Textutils.isempty (MPOIBTN)) {
Poibtn.settext (MPOIBTN);
Poibtn.setonclicklistener (Mpoilistener);
} else {
Poibtn.setvisibility (View.gone);
}

if (! Textutils.isempty (MNEGBTN)) {
Negbtn.settext (MNEGBTN);
Negbtn.setonclicklistener (Mneglistener);
} else {
Negbtn.setvisibility (View.gone);
}

Dialog.setcancelable (TRUE);
Dialog.setcanceledontouchoutside (TRUE);
return this;
}
int i = 0;
Public Customdialog Show () {
Window Mwin = Dialog.getwindow ();
Dialog.show ();
Dialog.setcontentview (Layout, new Viewgroup.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT));
Switch (animatetype) {
Case Low_disappear_animation:
Mwin.setwindowanimations (r.style.dialog_low_appear_animation);
Break
Case Rotate_disappear_animation:
Mwin.setwindowanimations (r.style.dialog_rotate_appear_animation);
Break
}

return dialog;
}

Public Builder setdisappearanimator (int animatetype) {
This.animatetype = Animatetype;
return this;
}

}
}

The code is not much, do a classification, the main is 3 parts: Building builder, Content settings, Dilalog display

First of all, the builder building, the construction method is mainly completed 2 functions, customdialog and layout of the filling, there is a point to note, do not hurry
The filled layout is added to the dialog instance, called Setcontentview or Addcontentview before the dialog Show method, which results in an error requestfeature must be called BEFO Re adding content;

The code of the content setting, mainly adopts a kind of chain design method, each method can return the Builder class itself, so it can be a straight line call method, so it is relatively concise;

Finally, it is the dialog display, remember to call Setcontentview or Addcontentview after show, and put it in setcancelable () and Setcancelableontouchoutside () This is the dialog disappears when the setting, the main two Boolean value, you can see for yourself;

Here is the layout of R.layout.custom_dialog1

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:background= "@drawable/shape_custom_bg_grey" >
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "40DP"
android:gravity= "Center" >
<imageview
Android:id= "@+id/title_icon"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
/>
<textview
Android:id= "@+id/title"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Place the default title here"
Android:textsize= "20sp"/>
</LinearLayout>
<textview
Android:id= "@+id/custom_content"
Android:layout_width= "Match_parent"
android:layout_height= "120DP"
Android:padding= "10DP"
Android:text= "Here you can put the body content"
Android:textalignment= "Center"
Android:textsize= "16sp"/>
<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "40DP" >
<textview
Android:id= "@+id/custom_cancel"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
Android:text= "Cancel"
Android:textsize= "18SP"
android:gravity= "Center"
android:background= "@color/cancelbtn"
/>
<textview
Android:id= "@+id/custom_confirm"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
Android:text= "OK"
Android:textsize= "18SP"
android:gravity= "Center"
android:background= "@color/confirmbtn"
/>
</LinearLayout>

</LinearLayout>

At the end, add a Dialog of the entry animation and the definition method of the Appearance animation

Dialog is mainly attached to a Window object to display hidden, window object acquisition can be obtained through Dialog.getwindow, so, Dialog in and out of the field animation is actually Window's entry and exit animation, then it is simple, through the XML to Custom

First is the Res/values/style.xml file

<style name= "dialog_low_appear_animation" parent= "Android:animation" >
<item name= "Android:windowenteranimation" > @anim/dialog_low_enter_animation</item>
<item name= "Android:windowexitanimation" > @anim/dialog_low_exit_animation</item>
</style>

Then, obviously after the Res/anim under the new dialog_low_enter_animation and dialog_low_exit_animation Resource file;

<?xml version= "1.0" encoding= "Utf-8"?>
<set xmlns:android= "Http://schemas.android.com/apk/res/android" >
<translate android:fromxdelta= "0" android:toxdelta= "0" android:fromydelta= "100%" android:toydelta= "0"
Android:duration= "/>"
<alpha android:fromalpha= "0" android:toalpha= "1" android:duration= "/>"

</set>

The parameters of entry and exit look at how to define the appropriate, and not much to say.

Attach the method of use

Customdialog dialog = new Customdialog.builer (). Settitle (""). SetContent ("")
. Setpositivebutton ("OK", new View.onclicklisterner () {
@override
public void OnClick (view view) {
Dialog.dismiss ();
}
})
. Setnegativebutton ("Cancel", new View.onclicklistener () {
@override
public void OnClick (view view) {
Dialog.dismiss ();
}
})
. Setdisappearanimation (CustomDialog.Builder.LOW_DISAPPEAR_ANIMATION)
. creat (). Show ();

Write here, OK, the usage of dialog is basically finished, about the principle of things are not yet involved, and later to go on to add.

How to implement Android custom dialog

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.