1. 程式人生 > >自定義dialog(建造者模式)

自定義dialog(建造者模式)

效果圖:

在這裡插入圖片描述

Styles.xml

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

colors.xml

   <color name="font_common_1">#424242</color>
    <color name="font_common_2">#a1a1a1</color>
    <color name="font_blue">#42369a</color>
    <color name="black">#000000</color>
    <color name="commom_background">#f3f3f3</color>
    <color name="white">#FFFFFF</color>

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="20dp"/>
</shape>

dialog_update.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_round_white"
    android:orientation="vertical">

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

    <TextView
        android:id="@+id/tv_version_now"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:gravity="center_horizontal"
        android:text="當前版本:V1.2.0"
        android:textColor="#333C4A"
        android:textSize="16sp" />

    <TextView
        android:visibility="gone"
        android:id="@+id/tv_version_new"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:lineSpacingExtra="3dp"
        android:text="新版本:V1.2.0"
        android:textColor="@color/font_common_1"
        android:textSize="16sp" />
    <TextView
        android:id="@+id/tv_content_new"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:lineSpacingExtra="3dp"
        android:text="更新內容:V1.2.0"
        android:textColor="#333C4A"
        android:textSize="16sp" />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/commom_background" />

    <LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/cancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:gravity="center"
            android:text="取消"
            android:textColor="#999BA0"
            android:textSize="16sp" />

        <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:layout_weight="1.0"
            android:gravity="center"
            android:text="確定"
            android:textColor="#BF40FF"
            android:textSize="16sp" />

    </LinearLayout>


</LinearLayout>

UpdateDialog.java

package test.hu.com.myapplication;

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

public class UpdateDialog extends Dialog implements View.OnClickListener {
    private TextView titleTxt;
    private TextView submitTxt;
    private TextView cancelTxt;
    private TextView nowVersionTxt;
    private TextView newVersionTxt;
    private TextView newContentTxt;
    private onClickListener listener;
    private String positiveName;
    private String negativeName;
    private String title;
    private int layout;
    private String nowVersion;
    private String newVersion;
    private String newContent;
    private boolean isCancel;

    @Override
    protected void onStart() {
        super.onStart();
        this.getWindow().setDimAmount(0f);
    }

    private UpdateDialog(Builder builder) {
        super(builder.mContext, builder.mThemeId);
        this.layout = builder.layout;
        this.title = builder.title;
        this.listener = builder.listener;
        this.positiveName = builder.positiveName;
        this.negativeName = builder.negativeName;
        this.nowVersion = builder.nowVersion;
        this.newVersion = builder.newVersion;
        this.newContent = builder.newContent;
        this.isCancel = builder.isCancel;
    }


    public static class Builder {
        private Context mContext;
        private String content;
        private onClickListener listener;
        private String positiveName;
        private String negativeName;
        private String title;
        private int layout;
        private int mThemeId;
        private String nowVersion;
        private String newVersion;
        private String newContent;
        private boolean isCancel;

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

        public Builder setCancel(boolean isCancel) {
            this.isCancel = isCancel;
            return this;
        }

        public Builder setNowVersion(String nowVersion) {
            this.nowVersion = nowVersion;
            return this;
        }

        public Builder setNewVersion(String newVersion) {
            this.newVersion = newVersion;
            return this;
        }

        public Builder setNewContent(String newContent) {
            this.newContent = newContent;
            return this;
        }

        public Builder setThemeId(int themeId) {
            this.mThemeId = themeId;
            return this;
        }

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

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

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

        public Builder setcontent(String content) {
            this.content = content;
            return this;
        }

        public Builder setLayout(int layout) {
            this.layout = layout;
            return this;
        }

        public Builder setOnClickLisener(onClickListener lisener) {
            this.listener = lisener;
            return this;
        }

        public UpdateDialog build() {
            return new UpdateDialog(this);
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout);
        setCanceledOnTouchOutside(false);
        initView();
        this.setCancelable(isCancel);
    }

    private void initView() {
        titleTxt = (TextView) findViewById(R.id.title);
        submitTxt = (TextView) findViewById(R.id.submit);
        submitTxt.setOnClickListener(this);
        cancelTxt = (TextView) findViewById(R.id.cancel);
        cancelTxt.setOnClickListener(this);
        nowVersionTxt = (TextView) findViewById(R.id.tv_version_now);
        newVersionTxt = (TextView) findViewById(R.id.tv_version_new);
        newContentTxt = (TextView) findViewById(R.id.tv_content_new);
        if (!TextUtils.isEmpty(nowVersion)) {
            nowVersionTxt.setText(nowVersion);
        }
        if (!TextUtils.isEmpty(newVersion)) {
            newVersionTxt.setText(newVersion);
        }
        if (!TextUtils.isEmpty(newContent)) {
            newContentTxt.setText(newContent);
        }
        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.onCancleClick(this);
                }
                break;
            case R.id.submit:
                if (listener != null) {
                    listener.onConfirmClick(this);
                }
                break;
        }
    }

    public interface onClickListener {
        void onConfirmClick(Dialog dialog);

        void onCancleClick(Dialog dialog);
    }
}

MainActivity.java

 UpdateDialog.Builder builder = new UpdateDialog.Builder()
                .setContext(this)
                .setTitle("標題")
                .setLayout(R.layout.dialog_update)
                .setThemeId(R.style.updateStyleDialog)
                .setNowVersion("現在版本")
                .setNewVersion("新版本")
                .setNewContent("新版本描述")
                .setNegativeButton("取消按鈕")
                .setPositiveButton("確定按鈕")
                .setCancel(false)//是否強制升級
                .setOnClickLisener(new UpdateDialog.onClickListener() {
                    @Override
                    public void onConfirmClick(Dialog dialog) {
                        showToast("確定");
                        dialog.dismiss();
                    }

                    @Override
                    public void onCancleClick(Dialog dialog) {
                        showToast("取消");
                        dialog.dismiss();
                    }
                });
        builder.build().show();

Demo地址