android kotlin 自定义dialog

代码如下 :






class ClearCacheDialog {
    private var context: Context? = null
    private var dialog: Dialog? = null
    private var btn_cancel: Button? = null
    private  var txt_updata_message:TextView? = null
    private var btn_update: Button? = null

    /**
     * init the dialog
     *
     * @return
     */
    fun MyVersionDialog(activity: Activity) {
        context = activity
        dialog = Dialog(context!!, R.style.mydialog_style)
        dialog!!.setContentView(R.layout.clear_cache_dialog)
        //设置dialog以外的不能点击
        dialog!!.setCancelable(false)
        txt_updata_message = dialog!!.findViewById(R.id.txt_updata_message) as TextView
        btn_update = dialog!!.findViewById(R.id.btn_update) as Button
        btn_cancel = dialog!!.findViewById(R.id.btn_cancel) as Button
        btn_cancel!!.setOnClickListener {
            //取消
         clearCache?.cancel()
            dismiss()

        }
        btn_update!!.setOnClickListener {
            //清除缓存
           clearCache?.clearCache()
                dismiss()
        }

    }

    var clearCache: ClearCacheOnClickCallBack?=null
    fun setClearCacheOnClickCallBack(clearCacheOnClickCallBack: ClearCacheOnClickCallBack) {
        this.clearCache = clearCacheOnClickCallBack
    }


    /**
     * Get the Text of the EditText
     */
    fun getText(): String? {
        //return editText.getText().toString();
        return null
    }

    public fun show() {
        dialog?.show()
    }

    fun hide() {
        dialog?.hide()
    }

    fun dismiss() {
        dialog?.dismiss()
    }

}
clear_cache_dialog.xml代码如下


    

    

    

    
        

            

styles.xml代码如下

ClearCacheOnClickCallBack接口如下


interface ClearCacheOnClickCallBack {
    fun clearCache()
    fun cancel()
}

activity代码如下

 var clear:ClearCacheDialog=ClearCacheDialog()
        clear.MyVersionDialog(this)
        clear .setClearCacheOnClickCallBack(this)

        clearCache.setOnClickListener{
            clear.show()

        }

 实现接口两个方法

class SettingAct: BaseActivity(), ClearCacheOnClickCallBack {
override fun clearCache() {
    //清除缓存
    Toast.makeText(activity,"清除成功",Toast.LENGTH_SHORT).show()
}

override fun cancel() {
    //退出
    Toast.makeText(activity,"取消",Toast.LENGTH_SHORT).show()
}
}

你可能感兴趣的:(android开发,android,kotlin,开发语言)