简介
使用kotlin的扩展函数实现,使用起来非常的简单,只需要在使用的地方调用即可,例如:
第一:更新提示框
效果图:
第二:等待条(菊花)
效果图:
第三:消息对话框
效果图:
扩展函数:
package com.example.test3
import android.app.AlertDialog
import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import kotlinx.android.synthetic.main.alterdialog_yes_no.view.*
import kotlinx.android.synthetic.main.fragment_updatin.view.*
private var mAlertDialog: AlertDialog? = null
private var mDialogFragment: DialogFragment? = null
/**
* 1、菊花--弹出耗时对话框
* @param context
*/
fun Context.showWaitDialog(tip: String) {
var tip = tip
if (TextUtils.isEmpty(tip)) {
tip = ""
}
if (mAlertDialog == null || this != mAlertDialog!!.context) {//mAlertDialog里面的context如果不同,也重新new,这样避免了第一个activity用了之后,第二个activity用mAlertDialog就不显示
mAlertDialog = AlertDialog.Builder(this, R.style.CustomProgressDialog).create()//去除AlertDialog的背景透明
}
val loadView = LayoutInflater.from(this).inflate(R.layout.dialog_progressbar_juhua, null)//默认的红色转圈
mAlertDialog!!.setView(loadView, 0, 0, 0, 0)
mAlertDialog!!.setCanceledOnTouchOutside(true)//随便点一下屏幕是否消失
val tvTip = loadView.findViewById<TextView>(R.id.tvTip)
tvTip.text = tip
mAlertDialog!!.show()
}
/**
* 2、弹出提示对话框(左上角X,中间文字,右下YES、NO)
* @param context
* @param hint 提示语
* @param type 0:只有ready的对话框;1:有YES、NO的对话框
*
* 发送心跳包出错会进入,连接出错会进入
*/
fun Context.showDialogYN( hintText: String, mListen: (String) -> Unit) {
if (mAlertDialog != null&& this == mAlertDialog!!.context && mAlertDialog!!.isShowing ) {
//表示在同一个activity,已经显示了,就不再显示
return
}
if (mAlertDialog == null || this != mAlertDialog!!.context) {
mAlertDialog = AlertDialog.Builder(this, R.style.TransparentDialog).create()//背景透明的dialog
}
val view1 = View.inflate(this, R.layout.alterdialog_yes_no, null)//有ready
mAlertDialog!!.setView(view1)
mAlertDialog!!.show()
view1.tv_message.text = hintText
view1.btn_confirm.setOnClickListener {
//按了yes按钮
mAlertDialog!!.dismiss()
// mListen("yes")
}
view1.btn_cancel.setOnClickListener {
//按了no按钮
mAlertDialog!!.dismiss()
}
}
/**
* 3、显示 提示更新的对话框
*/
fun Context.showUpdatinDialog() {
if (mAlertDialog == null || this != mAlertDialog!!.context) {//mAlertDialog里面的context如果不同,也重新new,这样避免了第一个activity用了之后,第二个activity用mAlertDialog就不显示
mAlertDialog =
AlertDialog.Builder(this, R.style.CustomProgressDialog).create()//去除AlertDialog的背景透明
}
val loadView = LayoutInflater.from(this).inflate(R.layout.fragment_updatin, null)//默认的红色转圈
mAlertDialog!!.setView(loadView, 0, 0, 0, 0)
mAlertDialog!!.setCanceledOnTouchOutside(true)//随便点一下屏幕是否消失
mAlertDialog!!.show()//显示
// 右上角关闭
loadView.close_dialog.setOnClickListener {
Log.d("showUpdatinDialog", "按了ready")
mAlertDialog!!.dismiss()
}
// 其他的3个按钮也是一样的写法
}
调用
bt1.setOnClickListener {
showWaitDialog("请稍候...")
}
bt2.setOnClickListener {
showDialogYN("确定要退出吗?") {
}
}
bt3.setOnClickListener {
showUpdatinDialog()
}
代码下载地址:https://download.csdn.net/download/wy313622821/13121959
第四 . 倒计时弹窗
效果图为:
详细的代码:下载
第五、自定义弹窗
效果图:
/**
* 作者:WY
* 创建时间: 13:59
* 功能模块说明:
*/
public class DialogDoubleBtn {
private AlertDialog mAlertDialog;
private WeakReference<Context> mContextWeak;
private WeakReference<View> mViewWeak;
private WeakReference<TextView> mPositiveButtonWeak;
private WeakReference<TextView> mNegativeButtonWeak;
private TextView tvHint;
public DialogDoubleBtn(Context context) {
mContextWeak = new WeakReference<>(context);
mViewWeak = new WeakReference<>(LayoutInflater.from(context).inflate(R.layout.dialog_double_btnnew, null));
tvHint = mViewWeak.get().findViewById(R.id.dialog_double_btn_tv_hint_content);
mAlertDialog = new AlertDialog.Builder(context).setView(mViewWeak.get()).create();
mPositiveButtonWeak = new WeakReference<>(mViewWeak.get().findViewById(R.id.dialog_double_btn_btn_right));
mNegativeButtonWeak = new WeakReference<>(mViewWeak.get().findViewById(R.id.dialog_double_btn_btn_left));
mPositiveButtonWeak.get().setOnClickListener(view -> dismiss());
mNegativeButtonWeak.get().setOnClickListener(view -> dismiss());
mAlertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "", (DialogInterface.OnClickListener) null);
mAlertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "", (DialogInterface.OnClickListener) null);
mAlertDialog.getWindow().setBackgroundDrawableResource(R.color.color_transparent); // 背景透明
}
public DialogDoubleBtn setContext(String tvHint) {
this.tvHint.setText(tvHint);
return this;
}
public DialogDoubleBtn setOkText(String okText) {
mPositiveButtonWeak.get().setText(okText);
return this;
}
public DialogDoubleBtn setError(String text) {
TextView tvError = mViewWeak.get().findViewById(R.id.dialog_double_btn_tv_error_content);
tvError.setText(text);
tvError.setVisibility(View.VISIBLE);
return this;
}
public void show() {
Context context = mContextWeak.get();
if (context == null) {
return;
}
mAlertDialog.show();
}
/**
* 确定按钮 点击事件
*
* @param listener
* @return
*/
public DialogDoubleBtn setOkListener(View.OnClickListener listener) {
TextView button = mPositiveButtonWeak.get();
if (button == null) {
return this;
}
button.setOnClickListener(view -> {
dismiss();
listener.onClick(button);
});
return this;
}
/**
* 设置点击外部可取消
*
* @param cancelable
* @return
*/
public DialogDoubleBtn setCancelable(boolean cancelable) {
if (mAlertDialog != null) {
mAlertDialog.setCancelable(cancelable);
mAlertDialog.setCanceledOnTouchOutside(cancelable);
}
return this;
}
/**
* 取消按钮 点击事件
*
* @param listener
* @return
*/
public DialogDoubleBtn setCancelListener(View.OnClickListener listener) {
TextView button = mNegativeButtonWeak.get();
if (button == null) {
return this;
}
button.setOnClickListener(view -> {
mAlertDialog.dismiss();
listener.onClick(button);
});
return this;
}
public void dismiss() {
if (mAlertDialog != null) {
mAlertDialog.dismiss();
mAlertDialog = null;
}
if (mContextWeak != null) {
mContextWeak.clear();
mContextWeak = null;
}
if (mViewWeak != null) {
mViewWeak.clear();
mViewWeak = null;
}
if (mPositiveButtonWeak != null) {
mPositiveButtonWeak.clear();
mPositiveButtonWeak = null;
}
if (mNegativeButtonWeak != null) {
mNegativeButtonWeak.clear();
mNegativeButtonWeak = null;
}
}
}
xml文件代码为:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_round_main_5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/dialog_double_btn_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/dp_30"
android:layout_marginTop="@dimen/dp_20"
android:text="温馨提示"
android:textColor="@color/color_white"
android:textSize="@dimen/sp_28"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgv"
android:layout_width="@dimen/dp_150"
android:layout_height="@dimen/dp_80"
android:layout_marginTop="@dimen/dp_4"
android:src="@mipmap/ic_tips_img"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/ll_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/dialog_double_btn_tv_title"
android:layout_marginTop="@dimen/dp_20"
android:background="@color/color_white"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/dp_40"
app:layout_constraintTop_toBottomOf="@+id/dialog_double_btn_tv_title">
<TextView
android:id="@+id/dialog_double_btn_tv_hint_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:text="11234"
android:textColor="@color/color_9BA3AC"
android:textSize="@dimen/sp_32" />
<TextView
android:id="@+id/dialog_double_btn_tv_error_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="45dp"
android:layout_marginRight="45dp"
android:visibility="gone"
android:text="11234"
android:textColor="@color/color_F55D3E"
android:textSize="@dimen/sp_32" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ll_msg"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="2"
app:layout_constraintTop_toBottomOf="@+id/ll_msg">
<TextView
android:id="@+id/dialog_double_btn_btn_left"
android:layout_width="0dp"
android:layout_height="@dimen/dp_70"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="@color/color_main"
android:gravity="center"
android:text="取消"
android:textColor="@color/color_white"
android:textSize="@dimen/sp_24" />
<TextView
android:id="@+id/dialog_double_btn_btn_right"
android:layout_width="0dp"
android:layout_height="@dimen/dp_70"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="@color/color_main"
android:gravity="center"
android:text="确定"
android:textColor="@color/color_white"
android:textSize="@dimen/sp_24" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>