欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kotlin 自定义AlertDialog

程序员文章站 2022-07-02 19:26:37
...

 最近写的一个简易选择框

 长这个样子:

 Kotlin 自定义AlertDialog

有多种模式:

1.输入框模式,有一个标题一个输入框。见上图(构造方法中 isEdit 字段 true的时候是开启输入框)

2.还有一种提示模式,有一个标题一个内容。

扩展:更多模式 比如  一个标题一个内容一个输入框,等。。。  

/**
 * Created by Xinghai.Zhao on 2020/8/4.
 * 自定义选择弹框
 */
class MyDialog(context: Context?) : AlertDialog(context), View.OnClickListener {

    var call: clickCallBack? = null;
    var TextView_title: TextView? = null;
    var TextView_content: TextView? = null;
    var editText: EditText? = null;
    var yesButton: View? = null;
    var noButton: View? = null;

    constructor(context: Context?, title: String, content: String, yesCallBack: clickCallBack)
            : this(context, title, content, yesCallBack, false) {
    }

    constructor(context: Context?, title: String, content: String, yesCallBack: clickCallBack, isEdit: Boolean) : this(context) {
        call = yesCallBack
        if (title != null) TextView_title?.setText(title);
        if (content != null) TextView_content?.setText(title);
        if (isEdit){
            TextView_content?.visibility = View.GONE
            editText?.visibility=View.VISIBLE
            val filters = arrayOf<InputFilter>(InputFilter.LengthFilter(10)) // 最大输入长度
            editText?.filters = filters
            editText?.isFocusable = true
            editText?.isFocusableInTouchMode = true
            editText?.requestFocus()
            editText?.keyListener = object : DigitsKeyListener() {
                override fun getInputType(): Int {
                    return InputType.TYPE_TEXT_VARIATION_PASSWORD
                }

                override fun getAcceptedChars(): CharArray {
                    return context?.resources!!.getString(R.string.edit_input)?.toCharArray()
                }
            }
        }

    }

    init {
        val inflate = LayoutInflater.from(context).inflate(R.layout.dialog_my, null);
        setView(inflate)
        //设置点击别的区域不关闭页面
        setCancelable(false)

        TextView_title = inflate.findViewById<TextView>(R.id.dialog_my_title)
        TextView_content = inflate.findViewById<TextView>(R.id.dialog_my_content)
        editText = inflate.findViewById<EditText>(R.id.dialog_my_edit)
        yesButton = inflate.findViewById<View>(R.id.dialog_my_yes)
        noButton = inflate.findViewById<View>(R.id.dialog_my_no)

        yesButton?.setOnClickListener(this)
        noButton?.setOnClickListener{dismiss()}
    }


    override fun onClick(p0: View?) {
        call?.yesClick(this);
    }

    interface clickCallBack {
        fun yesClick(dialog:MyDialog)
    }


}

外部调用代码

            /*
             *作者:赵星海
             *时间:2020/6/17 11:07
             *用途: 输入台牌号
             */
            val myDialog =MyDialog(activity,"请输入台牌号","",object :MyDialog.clickCallBack{
                override fun yesClick(dialog: MyDialog) {
                    val input:String =dialog.editText?.text.toString()
                    if (input==null||input .equals("")) {
                        Toast.makeText(activity, "台牌号不能为空!", Toast.LENGTH_LONG).show()
                    } else {
                        //此处写后续逻辑
                        dialog?.dismiss()
                    }
                }
            },true)
            myDialog.setOnDismissListener {
                //关闭监听逻辑
            }
            //显示的位置居中提高100dp-------------
            val w = myDialog.window
            val lp = w!!.attributes
            lp.x = 0
            lp.y = -100
            //-----------------------------------

            myDialog.show();
            //显示的宽高根据屏幕半分比调整-----------------------------------
            val m = activity!!.windowManager
            val d = m.defaultDisplay //为获取屏幕宽、高

            val p = myDialog.window!!.attributes //获取对话框当前的参数值

            p.height = (d.height * 0.40).toInt() //高度设置为屏幕的0.3

            p.width = (d.width * 0.45).toInt() //宽度设置为屏幕的0.5

            myDialog.window!!.attributes = p
            //--------------------------------------------------------------

下面加分割线的两段代码,其功能我已经写注释咯!