1. 效果
2. 调用方式
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 {
/**
* 三种图标状态
*/
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 图标类型
* @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();
}
}
});
}
/**
* 默认倒计时,时间长度为 5 秒
*
* @param context context
* @param message 消息
* @param iconType 图标类型
* @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>