Dialog共通写法(两个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 ConfirmDialog : Dialog {

private var supContext: Context? = null
private var dialogMsg: String? = ""
private var cacelButtonText: String? = ""
private var confirmButtonText: String? = ""
private var imageRes: Int = 0
private var backFlag: Boolean = true

constructor(supContext: Context, dialogMsg: String, cacelButtonText: String, confirmButtonText: String) : super(supContext) {
this.supContext = supContext
this.dialogMsg = dialogMsg
this.cacelButtonText = cacelButtonText
this.confirmButtonText = confirmButtonText
this.backFlag = backFlag
}

constructor(supContext: Context, dialogMsg: String, cacelButtonText: String, confirmButtonText: String, backFlag: Boolean) : super(supContext) {
this.supContext = supContext
this.dialogMsg = dialogMsg
this.cacelButtonText = cacelButtonText
this.confirmButtonText = confirmButtonText
this.backFlag = backFlag
}

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

}

constructor(supContext: Context, dialogMsg: String, cacelButtonText: String, confirmButtonText: String, backFlag: Boolean, imageRes: Int) : super(supContext) {
this.supContext = supContext
this.dialogMsg = dialogMsg
this.cacelButtonText = cacelButtonText
this.confirmButtonText = confirmButtonText
this.backFlag = backFlag
this.imageRes = imageRes
}

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

interface ClickListenerInterface {
//確認
fun doConfirm()

fun doCancel()
}

override fun onBackPressed() {
if (backFlag) {
dismiss()
}
return
}

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.confirm_alert_dialog, null)
setContentView(view)
//メッセージ
val tvMsg = view.findViewById(R.id.confirm_msg_text) as TextView
//確認
val tvConfirm = view.findViewById(R.id.click_btn) as Button
//メッセージ
val tvCancel = view.findViewById(R.id.cancel_btn) as Button
//画像
val dialogImage = view.findViewById(R.id.dialog_image) as ImageView
if (imageRes == 0) {
dialogImage.setImageDrawable(supContext!!.resources.getDrawable(R.mipmap.info))
} else {
dialogImage.setImageDrawable(supContext!!.resources.getDrawable(imageRes))
}
tvMsg.text = dialogMsg
tvConfirm.text = confirmButtonText
tvCancel.text = cacelButtonText

tvConfirm.setOnClickListener(clickListener())
tvCancel.setOnClickListener(clickListener())

setCancelable(false)
setCanceledOnTouchOutside(false)

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()
//ボタン
R.id.cancel_btn -> clickListenerInterface!!.doCancel()
}
}
}
}
怎么用:
val confirm = ConfirmDialog(this@MypageFragmentView, "ログアウトしてもよろしいでしょうか?", "いいえ", "はい")
confirm.setClicklistener(object : ConfirmDialog.ClickListenerInterface {
override fun doConfirm() {

logoutApi(this@MypageFragmentView)

confirm.dismiss()
}

//いいえ
override fun doCancel() {
confirm.dismiss()
}
})
confirm.show()

val confirmDialog = ConfirmDialog(this@MyThemeWebView, msg, "いいえ", "はい", false)

confirmDialog.setOnKeyListener { dialog, keyCode, event ->
if (keyCode == KeyEvent.KEYCODE_BACK && event!!.repeatCount == 0) {
handler.cancel()
}
false
}

confirmDialog.setClicklistener(object : ConfirmDialog.ClickListenerInterface {
override fun doConfirm() {
handler.proceed()
confirmDialog.dismiss()
}

//いいえ
override fun doCancel() {
handler.cancel()
confirmDialog.dismiss()
}
})

confirmDialog.show()


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