android自定义Dialog

来源:互联网 发布:html5注册登陆源码 编辑:程序博客网 时间:2024/06/04 00:58

自定义Dialog经常用到,这里记录一下。前面陈列一下需要做的准备工作,最后一步是使用方式。





    

----------------------------------------------------------------------------------------------------------------

<style name="MyDialog" parent="@style/Theme.AppCompat.Dialog">

        <!-- 背景颜色及透明程度  -->
        <item name="android:windowBackground">@android:color/transparent</item>
        <!--  是否有标题  -->
        <item name="android:windowNoTitle">true</item>
        <!--  是否浮现在activity之上  -->
        <item name="android:windowIsFloating">true</item>
        <!-- 是否模糊  -->
        <item name="android:backgroundDimEnabled">true</item>

    </style>

 ----------------------------------------------------------------------------------------------------------------

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;


import com.example.agent.R;


public class CommonDialog extends Dialog implements View.OnClickListener {
    TextView textTitle;
    TextView textContent;
    TextView textCancel;
    TextView textOk;


    private Context mContext;
    private int layoutId;
    private String content;
    private OnCloseListener listener;
    private String positiveName;
    private String negativeName;
    private String title;
    private boolean titleVisible = true;
    private boolean cancelVisible = true;


    public CommonDialog(Context context) {
        super(context);
        this.mContext = context;
    }


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


    public CommonDialog(Context context, int themeResId, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.listener = listener;


    }


    public CommonDialog(Context context, int layoutId, int themeResId, OnCloseListener listener) {
        super(context, themeResId);
        this.mContext = context;
        this.layoutId = layoutId;
        this.listener = listener;
    }


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


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




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


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


    public CommonDialog setTitleVisible(boolean show) {
        this.titleVisible = show;
        return this;
    }


    public CommonDialog setCancelVisible(boolean show) {
        this.cancelVisible = show;
        return this;
    }


    public CommonDialog setContent(String content) {
        this.content = content;
        return this;
    }


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


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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (layoutId != 0) {
            setContentView(layoutId);
        } else {
            setContentView(R.layout.dialog_common_1);
        }


        setCanceledOnTouchOutside(true);
        initView();
    }


    private void initView() {


        textContent = (TextView) findViewById(R.id.content);
        textTitle = (TextView) findViewById(R.id.title);
        textOk = (TextView) findViewById(R.id.text_ok);
        textCancel = (TextView) findViewById(R.id.text_cancel);


        textCancel.setOnClickListener(this);
        textOk.setOnClickListener(this);


        if (titleVisible) {
            textTitle.setVisibility(View.VISIBLE);
        } else {
            textTitle.setVisibility(View.GONE);
        }


        if (cancelVisible) {
            textCancel.setVisibility(View.VISIBLE);
        } else {
            textCancel.setVisibility(View.GONE);
        }


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


        if (!TextUtils.isEmpty(content)) {
            textContent.setText(content);
        }


        if (!TextUtils.isEmpty(positiveName)) {
            textOk.setText(positiveName);
        }


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




    }


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


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

------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@drawable/bg_round_white"
    android:orientation="vertical"
    android:padding="20dp"
   >


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/color_FF333333"
        android:textSize="@dimen/large"
        android:visibility="gone" />




    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:textColor="@color/color_FF333333"
        android:textSize="@dimen/medium" />


    <TextView
        android:id="@+id/text_cancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@mipmap/background_gray"
        android:gravity="center"
        android:padding="10dp"
        android:text="取消"
        android:textColor="@color/color_FF333333"
        android:visibility="visible" />


    <TextView
        android:id="@+id/text_ok"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@mipmap/background_authentication_primary"
        android:gravity="center"
        android:padding="10dp"
        android:text="确定"
        android:textColor="@color/color_FFFFFFFF" />


</LinearLayout>


------------------------------------------------------------------------------------------------------------------------------------

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


------------------------------------------------------------------------------------------------------------------------------------

使用:

 new CommonDialog(getActivity(), R.style.MyDialog, new CommonDialog.OnCloseListener() {
                            @Override
                            public void onClick(Dialog dialog, boolean confirm) {
                                 //To do what you want
                            }
                        }).setTitle(" 我是标题").setContent("我是内容").show();


可以设置标题,内容,按钮文字,一个按钮或两个按钮,这些都可以通过链式方式设置。