2026-11-28 00:25:22 +08:00
|
|
|
|
package com.focusbox.app.ui
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.Dialog
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
|
import android.graphics.Color
|
2026-11-28 11:35:03 +08:00
|
|
|
|
import android.graphics.Rect
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import android.graphics.drawable.ColorDrawable
|
|
|
|
|
|
import android.view.Gravity
|
|
|
|
|
|
import android.view.View
|
|
|
|
|
|
import android.view.ViewGroup
|
2026-11-28 11:35:03 +08:00
|
|
|
|
import android.view.ViewTreeObserver
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import android.view.WindowManager
|
2026-11-28 12:36:26 +08:00
|
|
|
|
import android.widget.FrameLayout
|
2026-11-28 00:25:22 +08:00
|
|
|
|
import com.focusbox.app.R
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 从底部升起的弹层。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 不用 Material 的 BottomSheetDialog:本工程刻意不引 Material 依赖(离线构建体积与
|
|
|
|
|
|
* 主题冲突都省了),而这里只需要"贴底 + 圆角 + 键盘顶起"三件事,自己写反而可控。
|
|
|
|
|
|
*/
|
|
|
|
|
|
open class SheetDialog(context: Context) : Dialog(context, R.style.SheetDialog) {
|
|
|
|
|
|
|
2026-11-28 12:36:26 +08:00
|
|
|
|
private var imeContainer: FrameLayout? = null
|
2026-11-28 11:35:03 +08:00
|
|
|
|
private var imeListener: ViewTreeObserver.OnGlobalLayoutListener? = null
|
2026-11-28 12:36:26 +08:00
|
|
|
|
private var fullHeight = false
|
2026-11-28 11:35:03 +08:00
|
|
|
|
|
2026-11-28 00:25:22 +08:00
|
|
|
|
init {
|
|
|
|
|
|
window?.apply {
|
|
|
|
|
|
setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
|
|
|
|
|
|
setGravity(Gravity.BOTTOM)
|
|
|
|
|
|
setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
|
|
|
|
|
|
attributes = attributes.apply {
|
|
|
|
|
|
width = ViewGroup.LayoutParams.MATCH_PARENT
|
|
|
|
|
|
dimAmount = 0.45f
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun setContentView(view: View) {
|
|
|
|
|
|
super.setContentView(view)
|
|
|
|
|
|
(view.parent as? View)?.setBackgroundColor(Color.TRANSPARENT)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-28 11:35:03 +08:00
|
|
|
|
/**
|
2026-11-28 12:36:26 +08:00
|
|
|
|
* 让带输入框的弹层跟随软键盘。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 关键点:窗口必须是"满屏 + 内容贴底",ADJUST_RESIZE 才有高度可缩。
|
|
|
|
|
|
* 之前用 WRAP_CONTENT 窗口 + ADJUST_NOTHING + 自己平移,在 EMUI/HarmonyOS 上
|
|
|
|
|
|
* 既拿不到正确的键盘高度,Dialog 本身也不被推动,输入框会被键盘盖住。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 现在双保险:
|
|
|
|
|
|
* 1. 标准路径靠 ADJUST_RESIZE 收缩满屏容器,贴底内容自动抬到键盘之上;
|
|
|
|
|
|
* 2. 某些 ROM 忽略 resize 时,按可见显示区域与容器高度的差值兜底平移。
|
2026-11-28 11:35:03 +08:00
|
|
|
|
*/
|
|
|
|
|
|
fun fitOnIme(content: View) {
|
2026-11-28 12:36:26 +08:00
|
|
|
|
val container = FrameLayout(context).apply {
|
|
|
|
|
|
layoutParams = ViewGroup.LayoutParams(
|
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
val lp = FrameLayout.LayoutParams(
|
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
|
|
|
Gravity.BOTTOM,
|
|
|
|
|
|
)
|
|
|
|
|
|
content.layoutParams = lp
|
|
|
|
|
|
container.addView(content)
|
|
|
|
|
|
setContentView(container)
|
|
|
|
|
|
fullHeight = true
|
|
|
|
|
|
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
|
|
|
|
|
|
|
|
|
|
|
|
val threshold = (context.resources.displayMetrics.density * 80).toInt()
|
2026-11-28 11:35:03 +08:00
|
|
|
|
val listener = object : ViewTreeObserver.OnGlobalLayoutListener {
|
|
|
|
|
|
override fun onGlobalLayout() {
|
|
|
|
|
|
val frame = Rect()
|
2026-11-28 12:36:26 +08:00
|
|
|
|
container.getWindowVisibleDisplayFrame(frame)
|
|
|
|
|
|
// resize 生效时容器已经收缩,差值为 0;未生效时差值就是键盘(含导航栏)高度
|
|
|
|
|
|
val keyboard = container.height - frame.bottom
|
2026-11-28 11:35:03 +08:00
|
|
|
|
content.translationY = if (keyboard > threshold) -keyboard.toFloat() else 0f
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-11-28 12:36:26 +08:00
|
|
|
|
container.viewTreeObserver.addOnGlobalLayoutListener(listener)
|
|
|
|
|
|
imeContainer = container
|
2026-11-28 11:35:03 +08:00
|
|
|
|
imeListener = listener
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun dismiss() {
|
2026-11-28 12:36:26 +08:00
|
|
|
|
imeListener?.let { imeContainer?.viewTreeObserver?.removeOnGlobalLayoutListener(it) }
|
2026-11-28 11:35:03 +08:00
|
|
|
|
imeListener = null
|
2026-11-28 12:36:26 +08:00
|
|
|
|
imeContainer = null
|
2026-11-28 11:35:03 +08:00
|
|
|
|
super.dismiss()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-28 00:25:22 +08:00
|
|
|
|
override fun show() {
|
|
|
|
|
|
super.show()
|
2026-11-28 12:36:26 +08:00
|
|
|
|
window?.setLayout(
|
|
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
|
|
|
if (fullHeight) ViewGroup.LayoutParams.MATCH_PARENT else ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
|
|
|
)
|
2026-11-28 00:25:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|