背景
记录实现符合项目需求的Toast弹窗
具体实现
kotlin
class MyTipDialog private constructor(val context: Activity): Dialog(context, R.style.MyTipTheme) {
val resId = ObservableField(0)
private val mainHandler = Handler(Looper.getMainLooper())
init {
setCanceledOnTouchOutside(true)
setCancelable(true)
window?.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
window?.setBackgroundDrawableResource(Color.parseColor("#00000000"))
window?.setWindowAnimations(R.style.myTipDialogAnim)
val binding = DataBindingUtil.inflate<MyTipsBinding>(layoutInflater, R.layout.my_tips, null, false)
binding.viewModel = this
setContentView(binding.root)
}
companion object{
private var instance: MyTipDialog ? = null
fun getInstance(context: Activity, resId: Int): MyTipDialog {
if (instance == null){
instance = MyTipDialog (context)
}
instance?.dismiss()
instance?.resId?.set(resId)
return instance!!
}
}
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
context.dispatchTouchEvent(ev)
return super.dispatchTouchEvent(ev)
}
override fun show() {
super.show()
mainHandler.postDelayed({
dismiss()
}, 2000)
}
override fun dismiss() {
super.dismiss()
mainHandler.removeCallbacksAndMessages(null)
}
}
自定义布局:R.layout.my_tips
xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.masked_boy.test.MyTipsDialog" />
</data>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="304dp"
android:paddingHorizontal="20dp"
android:paddingVertical="5dp"
android:layout_gravity="center"
android:text="@{context.getString(viewModel.resId)}"
android:textSize="11sp"
android:textColor="#FFFFFFFF"
android:background="#000000"/>
</FrameLayout>
</layout>
对话框style
xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="MyTipTheme" parent="AppTheme">
<item name="android:windowFullscreen">false</item>
<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>
</style>
<style name="myTipDialogAnim" mce_bogus="1" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/my_tip_dialog_enter_anim</item>
<item name="android:windowExitAnimation">@anim/my_tip_dialog_exit_anim</item>
</style>
</resources>
对话框show动画R.anim.my_tip_dialog_enter_anim
xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:interpolator="@android:anim/decelerate_interpolator">
<alpha
android:fromAlpha="0"
android:toAlpha="1" />
</set>
对话框dismiss动画R.anim.my_tip_dialog_exit_anim
xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:interpolator="@android:anim/decelerate_interpolator">
<alpha
android:fromAlpha="1"
android:toAlpha="0" />
</set>