自定义DialogAlert消息框

效果:

 

 

 

一、新建类CommomDialog 继承Dialog

public class CommomDialog extends Dialog implements View.OnClickListener{
    private TextView contentTxt;
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;

    private Context mContext;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;

    public CommomDialog(Context context) {
        super(context);
        this.mContext = context;
    }
    public CommomDialog(Context context, String content) {
        super(context, R.style.dialog);
        this.mContext = context;
        this.content = content;
    }

    public CommomDialog(Context context, int themeResId, String content) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
    }

    public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.content = content;
        this.listener = listener;
    }

    protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
    }

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

    public CommomDialog setPositiveButton(String name){
        this.positiveName = name;
        return this;
    }

    public CommomDialog setNegativeButton(String name){
        this.negativeName = name;
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_commom);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView(){
        contentTxt = (TextView)findViewById(R.id.content);
        titleTxt = (TextView)findViewById(R.id.title);
        submitTxt = (TextView)findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView)findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);

        contentTxt.setText(content);
        if(!TextUtils.isEmpty(positiveName)){
            submitTxt.setText(positiveName);
        }

        if(!TextUtils.isEmpty(negativeName)){
            cancelTxt.setText(negativeName);
        }

        if(!TextUtils.isEmpty(title)){
            titleTxt.setText(title);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancel:
                if(listener != null){
                    listener.onClick(this, false);
                }
                this.dismiss();
                break;
            case R.id.submit:
                if(listener != null){
                    listener.onClick(this, true);
                }
                break;
        }
    }

    public interface OnCloseListener{
        void onClick(Dialog dialog, boolean confirm);
    }
}

 

二、新建dialog_commom.xml布局

<?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:background="@drawable/bg_round_white"
    android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="12dp"
            android:layout_marginTop="12dp"
            android:text="提示"
            android:textSize="16sp"
            android:textColor="@color/black"/>


    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:lineSpacingExtra="3dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="40dp"
        android:layout_marginBottom="30dp"
        android:text="签到成功,获得200积分"
        android:textSize="12sp"
        android:textColor="@color/font_common_1"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/commom_background"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/cancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_dialog_left_white"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="取消"
            android:textSize="12sp"
            android:textColor="@color/font_common_2"/>

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@color/commom_background"/>

        <TextView
            android:id="@+id/submit"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg_dialog_right_white"
            android:gravity="center"
            android:layout_weight="1.0"
            android:text="确定"
            android:textSize="12sp"
            android:textColor="@color/font_blue"/>

    </LinearLayout>


</LinearLayout>

 

三、在drawable下新建三个xml

bg_dialog_left_white.xml

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

    <solid android:color="@color/white" />
    <corners android:bottomLeftRadius="8dp" />
</shape>

 

bg_dialog_right_white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/white" />
    <corners android:bottomRightRadius="8dp" />
</shape>

 

bg_round_white.xml

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

    <solid android:color="@color/white" />
    <corners android:radius="8dp" />
</shape>

 

四、在styles.xml添加

<style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <!--边框-->
        <item name="android:windowIsFloating">true</item>
        <!--是否浮现在activity之上-->
        <item name="android:windowIsTranslucent">false</item>
        <!--半透明-->
        <item name="android:windowNoTitle">true</item>
        <!--无标题-->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--背景透明-->
        <item name="android:backgroundDimEnabled">true</item>
        <!--模糊-->

    </style>

 

五、在colors.xml中添加

 <color name="mainColor">#573567</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="font_gray_b">#d4d4d3</color>

    <color name="font_tab_1">#42369a</color>
    <color name="font_tab_0">#b1b1b1</color>


    <color name="font_common_1">#424242</color>
    <color name="font_common_2">#a1a1a1</color>
    <color name="font_blue">#42369a</color>

    <color name="font_green">#00cccc</color>

    <color name="commom_background">#f3f3f3</color>

 

 

六、触发

new CommomDialog(Home.this, R.style.dialog, "确认退出此程序?", new CommomDialog.OnCloseListener() {
                            @Override
                            public void onClick(Dialog dialog, boolean confirm) {
                                if (confirm) {
                                    ActivityCollector.FinishAll();
                                    dialog.dismiss();
                                }
                            }
                        }).setTitle("提示").show();

 

 

作者发布的源码:https://github.com/xiaoxiaoqingyi/mine-android-repository

posted @ 2017-08-23 16:29  XinYiBuFang  阅读(1505)  评论(0编辑  收藏  举报