kotlin好看的弹出框、自定义弹出框(对话框)、扩展函数、菊花等待条、消息提示框

2022-07-14 14:26:56

简介

使用kotlin的扩展函数实现,使用起来非常的简单,只需要在使用的地方调用即可,例如:

第一:更新提示框

效果图:

在这里插入图片描述

第二:等待条(菊花)

效果图:

在这里插入图片描述

第三:消息对话框

效果图:
在这里插入图片描述
扩展函数:

package com.example.test3import android.app.AlertDialogimport android.content.Contextimport android.text.TextUtilsimport android.util.Logimport android.view.LayoutInflaterimport android.view.Viewimport android.widget.TextViewimport androidx.fragment.app.DialogFragmentimport kotlinx.android.synthetic.main.alterdialog_yes_no.view.*import kotlinx.android.synthetic.main.fragment_updatin.view.*privatevar mAlertDialog: AlertDialog?=nullprivatevar mDialogFragment: DialogFragment?=null/**
 * 1、菊花--弹出耗时对话框
 * @param context
 */
fun Context.showWaitDialog(tip: String){var tip= tipif(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

  • 作者:wy313622821
  • 原文链接:https://blog.csdn.net/wy313622821/article/details/109766225
    更新时间:2022-07-14 14:26:56