kotlin自定义弹框dialog

核心代码我贴出来了:其他一些颜色图片资源,都在源码里面,需要的朋友请自行下载运行,这个程序是已经测试过的,亲测可运行

项目特色: 1.diallog是自定义的

2.是用kotlin代码写的 也比较常用

3.自带dialog dismiss监听 ,可以知道结束弹框  做了什么

4.去除了标题栏 

效果图:

kotlin自定义弹框dialog_第1张图片

 

源码地址:https://github.com/wrs13634194612/KotlinDialog

主界面:

package com.example.admin.zkotlin


import android.app.Activity
import android.graphics.drawable.AnimationDrawable
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import org.json.JSONObject
import android.content.DialogInterface
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_staff_detail.*


class  MainActivity : BaseActivity() ,View.OnClickListener {
    override fun initViews() {
    }

    override fun initEvents() {
    }

    lateinit var ICCardDialog: AutoHideBottomUIDialog
    private val dialogHeight = 460f
    lateinit var ivRFIDAnim: ImageView
    lateinit var tvRecordRFIDResult: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_staff_detail)
        initICCardDialog()

        tvBindRFID.setOnClickListener(this)
    }

    private fun initICCardDialog() {
        ICCardDialog = AutoHideBottomUIDialog(this)
        ICCardDialog.setContentView(R.layout.dialog_entry_ic_card)
        ICCardDialog.setDialogHeight(dialogHeight)
        ivRFIDAnim = ICCardDialog.findViewById(R.id.ivAnim)
        tvRecordRFIDResult = ICCardDialog.findViewById(R.id.tvRecordResult)


        ICCardDialog.setOnDismissListener(DialogInterface.OnDismissListener { dialog ->
            Log.i("TAG", "cancel  ICCardDialog dismiss")
        })

    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.tvBindRFID -> {
                tvRecordRFIDResult.text = "等待录入···"
                ICCardDialog.show()
            }

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (ICCardDialog.isShowing) {
            ICCardDialog.dismiss()
        }

    }
}

2.自定义的dialog

package com.example.admin.zkotlin


import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager

class AutoHideBottomUIDialog : Dialog {

    var mContext: Context

    constructor(context: Context) : super(context) { this.mContext = context}
    private var height = 0f

    override fun onStart() {
        super.onStart()
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }

    override fun show() {
        this.window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        this.window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        var window = this.window
        if(height == 0f){
            window.attributes.height = DisplayUtil.dip2px(mContext, 440f)
        }else {
            window.attributes.height = DisplayUtil.dip2px(mContext, height)
        }
        window.attributes.width = DisplayUtil.dip2px(mContext, 300f)
        window.setBackgroundDrawable(mContext.resources.getDrawable(R.color.color_transparent))
        window.setGravity(Gravity.CENTER)
        super.show()
    }

    fun setDialogHeight(width : Float) : AutoHideBottomUIDialog {
        this.height = width
        return this
    }

}

自定义弹框的布局




    

    

    

    

    

end

你可能感兴趣的:(android,学习)