Android custom Dialog tool class, androiddialog

Source: Internet
Author: User

Android custom Dialog tool class, androiddialog

Because of project requirements, the system pop-up box can no longer meet our needs, we need a variety of pop-up boxes, then we need to customize the pop-up box.

Create the layout file dialog_layout.xml and copy the following content.

<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">

<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "5dp"
Android: layout_marginRight = "5dp"
Android: layout_marginTop = "5dp"
Android: background = "@ drawable/shape_linearlayout"
Android: orientation = "vertical"
>

<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "150dp"
Android: gravity = "center_vertical"
Android: orientation = "vertical">

<TextView
Android: id = "@ + id/txt_dialog_message"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center"
Android: layout_marginLeft = "20dp"
Android: layout_marginRight = "20dp"
Android: text = "TextView"/>
</LinearLayout>

<View
Android: layout_width = "match_parent"
Android: layout_height = "1dp"
Android: background = "@ color/line"/>

<LinearLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "5dp"
Android: layout_marginRight = "5dp"
Android: layout_marginBottom = "1dp">

<Button
Android: id = "@ + id/btn_dialog_go"
Android: layout_width = "0dp"
Android: layout_height = "40dp"
Android: layout_weight = "1"
Android: background = "@ drawable/linearlayout"
Android: text = "OK"
Android: textColor = "@ color/blue"/>

<View
Android: id = "@ + id/dialog_bg"
Android: layout_width = "1dp"
Android: layout_height = "40dp"
Android: background = "@ color/line"/>

<Button
Android: id = "@ + id/btn_dialog_add"
Android: layout_width = "0dp"
Android: layout_height = "40dp"
Android: layout_weight = "1"
Android: background = "@ drawable/linearlayout"
Android: text = "cancel"
Android: textColor = "@ color/blue"/>
</LinearLayout>
</LinearLayout>

</LinearLayout>

 

Create a java class DialogUtil. java

Public class DialogUtil {

Private static DialogUtil dialogUtil;
Private Dialog dialog;
Private TextView message;
Private Button firstBtn;
Private Button secondBtn;
Private onButtnClickListener buttnClickListener;

Public static DialogUtil getDialogInstance (Context context ){
If (null = dialogUtil ){
DialogUtil = new DialogUtil (context );
}
Return dialogUtil;
}

Private DialogUtil (Context context ){
InitDialog (context );
}

Public void setMessage (String message ){
If (null! = Message &&! Message. equals ("")){
This. message. setText (message );
}

}

Private void initDialog (Context context ){
Dialog = new Dialog (context );

Dialog. setContentView (R. layout. dialog_layout );
Dialog. setCanceledOnTouchOutside (false );
Window window = dialog. getWindow ();
Dialog. setCanceledOnTouchOutside (true );
WindowManager. LayoutParams params = window. getAttributes ();
Params. width = ScreenUtils. getScreenWidth (context)
-ScreenUtils. getScreenWidth (context)/4;
Params. height = LayoutParams. WRAP_CONTENT;
Params. gravity = Gravity. CENTER;
Window. setAttributes (params );
Window. setWindowAnimations (R. style. displayStyle );
Message = (TextView) dialog.findViewById(R.id.txt _ dialog_message );
FirstBtn = (Button) dialog. findViewById (R. id. btn_dialog_go );
SecondBtn = (Button) dialog. findViewById (R. id. btn_dialog_add );
FirstBtn. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
If (null! = ButtnClickListener)
ButtnClickListener. onFirstBtnClick (v );
}
});
SecondBtn. setOnClickListener (new OnClickListener (){

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
If (null! = ButtnClickListener)
ButtnClickListener. onSecondBtnClick (v );
}
});
}

Public void showDialog (){
If (dialog! = Null &&! Dialog. isShowing ()){
Dialog. show ();
}
}

Public void dissmissDialog (){
If (dialog! = Null & dialog. isShowing ()){
Dialog. dismiss ();

}
}

Public void setOnButtnClickListener (onButtnClickListener buttnClickListener ){
This. buttnClickListener = buttnClickListener;
}

Public interface onButtnClickListener {
Public void onFirstBtnClick (View v );

Public void onSecondBtnClick (View v );
}

}

The dialog tool class adopts the single-column mode, and the calling process is

1. Get the object first

DialogUtil. getDialogInstance (context );

2. Set text information

SetMessage (String message );

3. Set the single-host event of the button

SetOnButtnClickListener (onButtnClickListener buttnClickListener );

4. Display dialog

ShowDialog ();

Now the custom pop-up box is complete.

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.