Android自定义Dialog

效果图:
这里写图片描述
CustomDialog.java

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomDialog extends Dialog {

    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         * 
         * @param title
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         * 
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         * 
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         * 
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context.getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            final CustomDialog dialog = new CustomDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            // set the dialog title
            ((TextView) layout.findViewById(R.id.title)).setText(title);
            // set the confirm button
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {
                            negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
            }
            // set the content message
            if (message != null) {
                ((TextView) layout.findViewById(R.id.message)).setText(message);
            } else if (contentView != null) {
                // if no message set
                // add the contentView to the dialog body
                ((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content)).addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            }
            dialog.setContentView(layout);
            return dialog;
        }
    }
}

dialog_normal_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:orientation="vertical"
    android:padding="20.0dip" >

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/dialog_background"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="60dp"
            android:gravity="center"
            android:text="XXXXXXX"
            android:textColor="#ff666666"
            android:textSize="18dp"
            android:textStyle="bold"
            android:visibility="visible" />

        <LinearLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center" >

            <TextView
                android:id="@+id/message"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="top|center"
                android:lineSpacingMultiplier="1.2"
                android:minHeight="60.0dip"
                android:paddingBottom="5.0dip"
                android:paddingLeft="10.0dip"
                android:paddingRight="10.0dip"
                android:paddingTop="5.0dip"
                android:text="XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
                android:textColor="#ff666666"
                android:textSize="14dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="60.0dip"
            android:layout_gravity="bottom"
            android:gravity="center"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/negativeButton"
                style="@style/small_orange_btn_press"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="取消" />

            <Button
                android:id="@+id/positiveButton"
                style="@style/small_orange_btn_normal"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:text="确定" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

style.xml

<style name="large_orange_button">
        <item name="android:layout_width">300dp</item>
        <item name="android:layout_height">@dimen/large_button_hight</item>
        <item name="android:layout_marginLeft">@dimen/margin_left_distance</item>
        <item name="android:layout_marginRight">@dimen/margin_right_distance</item>
        <item name="android:layout_marginTop">@dimen/widget_spacing_hight</item>
        <item name="android:gravity">center</item>
        <item name="android:textColor">@drawable/btn_orange_text_color_press</item>
        <item name="android:textSize">@dimen/large_button_text_size</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@drawable/btn_orange_style</item>
    </style>

    <style name="small_orange_btn_press" parent="large_orange_button">
        <item name="android:layout_width">100dp</item>
        <item name="android:layout_marginTop">0dp</item>
        <item name="android:background">@drawable/btn_orange_style_press</item>
        <item name="android:textColor">@drawable/btn_orange_text_color_normal</item>
    </style>

    <style name="small_orange_btn_normal" parent="large_orange_button">
        <item name="android:layout_width">100dp</item>
        <item name="android:layout_marginTop">0dp</item>
        <item name="android:background">@drawable/btn_orange_style</item>
    </style>
    <style name="Dialog" parent="android:style/Theme.Dialog">
        <item name="android:background">#00000000</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>

btn_orange_text_color_press.xml

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  

    <item android:color="#db6200" android:state_focused="true" android:state_pressed="true"/>
    <item android:color="#db6200" android:state_focused="false" android:state_pressed="true"/>
    <item android:color="#db6200" android:state_focused="true"/>
    <item android:color="@android:color/white" android:state_focused="false"/>

</selector>  

btn_orange_style.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_orange_pressed" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_orange_pressed" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_orange_pressed" android:state_focused="true"/>
    <item android:drawable="@drawable/btn_orange_normal" android:state_focused="false"/>

</selector>

btn_orange_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <corners android:radius="50dp" />

    <stroke android:width="1px" android:color="#db6200" />

</shape>

btn_orange_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#fc8200" />

    <corners android:radius="50dp" />

</shape>

btn_orange_style_press.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_orange_normal" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_orange_normal" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_orange_normal" android:state_focused="true"/>
    <item android:drawable="@drawable/btn_orange_pressed" android:state_focused="false"/>

</selector>

btn_orange_text_color_normal.xml

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  

    <item android:color="@android:color/white" android:state_focused="true" android:state_pressed="true"/>
    <item android:color="@android:color/white" android:state_focused="false" android:state_pressed="true"/>
    <item android:color="@android:color/white" android:state_focused="true"/>
    <item android:color="#db6200" android:state_focused="false"/>

</selector>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玉辰56

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值