Dialog共通写法(两个button)
2024-09-04 22:39:19
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()
最新文章
- Scala中None, Nil, Nothing的区别
- DataTable的过滤需要的数据
- Markdown语言.md文件
- LuManager 2.0.97 设置shopex 手机版waptouch,绑定二级目录
- C#:向控件添加信息类
- cmd命令行中的errorlevel和延迟赋值
- 前端优化分析 之 javascript引用位置优化
- bzoj 1090 [SCOI2003]字符串折叠(区间DP)
- .NET编程和SQL Server ——Sql Server 与CLR集成 (学习笔记整理-1)
- C++STL vector简单使用练习1
- IPQ4028开启I2C功能
- vim之添加多行和删除多行
- HTML5效果:Canvas 实现圆形进度条并显示数字百分比
- 【2017下集美大学软工1412班_助教博客】团队作业7——第二次项目冲刺(Beta阶段)成绩公示
- Convolutional Neural Networks: Step by Step
- Web 应用架构基础课(转载)
- 监测多个textFiled.text的长度同时不小于0
- 瞥了一眼js
- HDU1576 A/B
- 《DSP using MATLAB》示例Example 8.17