Dialog共通写法(一个button)

一个button的:

package jp.co.hyakujushibank.view

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import jp.co.hyakujushibank.securestarter.R

@Suppress("DEPRECATION")
/**
*
* Created by liuhaolong on 2017/07/05.
*/
class AlertDialog : Dialog {
private var supContext: Context? = null
private var dialogMsg: String? = ""
private var buttonText: String? = "閉じる"
private var imageRes: Int = 0
private var titleFlag: Boolean = false

constructor(supContext: Context, dialogMsg: String) : super(supContext) {
this.supContext = supContext
this.dialogMsg = dialogMsg
this.buttonText = buttonText
}

constructor(supContext: Context, dialogMsg: String, buttonText: String) : super(supContext) {
this.supContext = supContext
this.dialogMsg = dialogMsg
this.buttonText = buttonText
}

constructor(supContext: Context, dialogMsg: String, buttonText: String, imageRes: Int) : super(supContext) {
this.supContext = supContext
this.dialogMsg = dialogMsg
this.buttonText = buttonText
this.imageRes = imageRes
}

constructor(supContext: Context, dialogMsg: String, buttonText: String, imageRes: Int, titleFlag: Boolean) : super(supContext) {
this.supContext = supContext
this.dialogMsg = dialogMsg
this.buttonText = buttonText
this.imageRes = imageRes
this.titleFlag = titleFlag
}


//クリックリスナーインターフェース
var clickListenerInterface: ClickListenerInterface? = null

interface ClickListenerInterface {
//確認
fun doConfirm()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
//初期化
init()

}

fun setClicklistener(clickListenerInterface: ClickListenerInterface) {
//クリックリスナーインターフェース
this.clickListenerInterface = clickListenerInterface
}

fun init() {
//充填画面
val inflater = LayoutInflater.from(supContext)
val view = inflater.inflate(R.layout.alert_dialog, null)
setContentView(view)
//メッセージ
val msgTitleText = view.findViewById(R.id.msg_title_text) as TextView
if (titleFlag) {
msgTitleText.visibility = View.VISIBLE
}
//メッセージ
val msgText = view.findViewById(R.id.confirm_msg_text) as TextView
//確認
val clickBtn = view.findViewById(R.id.click_btn) as Button
//画像
val dialogImage = view.findViewById(R.id.dialog_image) as ImageView
if (imageRes == 0) {
dialogImage.setImageDrawable(supContext!!.resources.getDrawable(R.mipmap.ic_error))
} else {
dialogImage.setImageDrawable(supContext!!.resources.getDrawable(imageRes))
}
//メッセージ
msgText.text = dialogMsg
//確認
clickBtn.text = buttonText
//許可キャンセル
setCancelable(false)
setCanceledOnTouchOutside(false)
//確認
clickBtn.setOnClickListener(clickListener())

val dialogWindow = window
val lp = dialogWindow.attributes
val d = context.resources.displayMetrics
lp.width = (d.widthPixels * 0.95).toInt()
dialogWindow.attributes = lp
}

inner class clickListener : View.OnClickListener {
override fun onClick(v: View) {
val id = v.id
when (id) {
//ボタン
R.id.click_btn -> clickListenerInterface!!.doConfirm()
}
}
}
}




如何用:
val alert = AlertDialog(activity, resposeAllTransaction.resultData!!.error_messege!!)
alert.setClicklistener(object : AlertDialog.ClickListenerInterface {
override fun doConfirm() {
alert.dismiss()
}
})
alert.show()
alertAgree = AlertDialog(this@RulesActivity, "利用規定に同意してからお進みください", "閉じる")
alertAgree!!.setClicklistener(object : AlertDialog.ClickListenerInterface {
override fun doConfirm() {
closeAlertAgree()
}
})

val alert = AlertDialog(this@CheckPassCodeActivity, "パスコードを5回間違えたので、ログアウトします。", "閉じる", R.mipmap.ic_error)
alert.setClicklistener(object : AlertDialog.ClickListenerInterface {
override fun doConfirm() {
Api.logoutForPasscodeApi(this@CheckPassCodeActivity)
alert.dismiss()
}
})
alert.show()

val alert = AlertDialog(context, msg, "閉じる", R.mipmap.info, true)
alert.setClicklistener(object : AlertDialog.ClickListenerInterface {
override fun doConfirm() {
alert.dismiss()
}
})




posted @ 2017-10-13 15:52  改变世界的老十七  阅读(650)  评论(0编辑  收藏  举报