倒計時 Dialog

1. 效果

1.png

2. 調(diào)用方式

                TimerDialogUtil.start(mContext, "message", TimerDialogUtil.ICON_SUCCESS, 5000, new TimerDialogUtil.OnTimerClickListener() {
                    @Override
                    public void finish() {
                        ToastUtils.showShort("here to do something ");
                    }
                });

3. Code

包含兩個文件鞠呈,分別是:代碼和頁面

  • TimerDialogUtil.java
package com.ums.testapi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Author:Created by Thorn on 2019/1/9
 * Function: alert a CountDownTimer Dialog
 */
@SuppressWarnings("WeakerAccess")
public class TimerDialogUtil {


    /**
     * 三種圖標(biāo)狀態(tài)
     */
    public static final int ICON_SUCCESS = 0;
    public static final int ICON_FAIL = 1;
    public static final int ICON_WARN = 2;

    /**
     * @param context    context
     * @param message    文字描述
     * @param iconType   圖標(biāo)類型
     * @param timerCount 毫秒為單位
     * @param onTimerClickListener  listener
     */
    public static void start(final Activity context, final String message, final int iconType, final int timerCount, final OnTimerClickListener onTimerClickListener) {
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(context);
        @SuppressLint("InflateParams") View view = LayoutInflater.from(context).inflate(R.layout.dialog_prompt, null);
        builder.setView(view);
        ImageView iv_icon = view.findViewById(R.id.iv_icon);
        final TextView tv_message = view.findViewById(R.id.tv_message);
        final TextView tv_confirm = view.findViewById(R.id.tv_confirm);
        switch (iconType) {
            case ICON_SUCCESS:
                iv_icon.setImageResource(R.mipmap.ic_dialog_success);
                break;
            case ICON_FAIL:
                iv_icon.setImageResource(R.mipmap.ic_dialog_error);
                break;
            case ICON_WARN:
                iv_icon.setImageResource(R.mipmap.ic_dialog_warn);
                break;
            default:
                iv_icon.setImageResource(R.mipmap.ic_dialog_warn);
                break;
        }

        tv_message.setText(message);
        builder.setCancelable(false);
        final android.support.v7.app.AlertDialog dialog = builder.show();

        final CountDownTimer countDownTimer = new CountDownTimer(timerCount, 1000) {
            @SuppressLint({"DefaultLocale", "SetTextI18n"})
            @Override
            public void onTick(long millisUntilFinished) {
                int remainTime = (int) (millisUntilFinished / 1000L);
                tv_message.setText(message);
                tv_confirm.setText("確定" + "(" + remainTime + "s)");
            }

            @Override
            public void onFinish() {
                if (dialog != null) {
                    dialog.cancel();
                }
                if (onTimerClickListener !=null) {
                    onTimerClickListener.finish();
                }
            }
        };
        countDownTimer.start();

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dialog != null) {
                    dialog.cancel();
                }
                //noinspection ConstantConditions
                if (countDownTimer != null) {
                    countDownTimer.cancel();
                }
                if (onTimerClickListener !=null) {
                    onTimerClickListener.finish();
                }
            }
        });
    }


    /**
     * 默認(rèn)倒計時,時間長度為 5 秒
     *
     * @param context  context
     * @param message  消息
     * @param iconType 圖標(biāo)類型
     * @param onTimerClickListener  listener
     */
    public static void start(final Activity context, final String message, final int iconType, final OnTimerClickListener onTimerClickListener) {
        int timerCount = 5000;
        start(context, message, iconType, timerCount, onTimerClickListener);
    }

    /**
     * 點擊接口類
     */
    public interface OnTimerClickListener{
        void finish();
    }
}


  • dialog_prompt.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:orientation="vertical">

    <ImageView
        android:contentDescription="@string/app_name"
        android:id="@+id/iv_icon"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="支付成功"
        android:textSize="18sp" />

    <!--分割線-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:layout_marginTop="20sp"
        android:background="@android:color/darker_gray" />


    <TextView
        android:id="@+id/tv_confirm"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="確定"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

</LinearLayout>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末右钾,一起剝皮案震驚了整個濱河市蚁吝,隨后出現(xiàn)的幾起案子旱爆,更是在濱河造成了極大的恐慌,老刑警劉巖窘茁,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件怀伦,死亡現(xiàn)場離奇詭異,居然都是意外死亡山林,警方通過查閱死者的電腦和手機房待,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來驼抹,“玉大人桑孩,你說我怎么就攤上這事】蚣剑” “怎么了流椒?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長左驾。 經(jīng)常有香客問我镣隶,道長,這世上最難降的妖魔是什么诡右? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任安岂,我火速辦了婚禮,結(jié)果婚禮上帆吻,老公的妹妹穿的比我還像新娘域那。我一直安慰自己,他們只是感情好猜煮,可當(dāng)我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布次员。 她就那樣靜靜地躺著,像睡著了一般王带。 火紅的嫁衣襯著肌膚如雪淑蔚。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天愕撰,我揣著相機與錄音刹衫,去河邊找鬼。 笑死搞挣,一個胖子當(dāng)著我的面吹牛带迟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播囱桨,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼仓犬,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了舍肠?” 一聲冷哼從身側(cè)響起搀继,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤窘面,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后律歼,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體民镜,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年险毁,在試婚紗的時候發(fā)現(xiàn)自己被綠了制圈。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡畔况,死狀恐怖鲸鹦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情跷跪,我是刑警寧澤馋嗜,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站吵瞻,受9級特大地震影響葛菇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜橡羞,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一眯停、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧卿泽,春花似錦莺债、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至第租,卻和暖如春措拇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背慎宾。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工儡羔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人璧诵。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像仇冯,于是被迫代替她去往敵國和親之宿。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,086評論 2 355