修复倒计时读数冻结、原因输入键盘遮挡、备注括号与任务备注截断

This commit is contained in:
XIGE
2026-11-28 12:36:26 +08:00
parent 5e352c646a
commit 7986a65dce
5 changed files with 83 additions and 28 deletions

View File

@@ -10,6 +10,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.WindowManager
import android.widget.FrameLayout
import com.focusbox.app.R
/**
@@ -20,8 +21,9 @@ import com.focusbox.app.R
*/
open class SheetDialog(context: Context) : Dialog(context, R.style.SheetDialog) {
private var imeRoot: View? = null
private var imeContainer: FrameLayout? = null
private var imeListener: ViewTreeObserver.OnGlobalLayoutListener? = null
private var fullHeight = false
init {
window?.apply {
@@ -41,36 +43,61 @@ open class SheetDialog(context: Context) : Dialog(context, R.style.SheetDialog)
}
/**
* 让带输入框的弹层跟随软键盘:弹出时整体上移,收起后回到页面底部。
* ADJUST_RESIZE 在部分 EMUI 上不会推动 Dialog 内容,只能自己按可见区域平移。
* 让带输入框的弹层跟随软键盘。
*
* 关键点:窗口必须是"满屏 + 内容贴底",ADJUST_RESIZE 才有高度可缩。
* 之前用 WRAP_CONTENT 窗口 + ADJUST_NOTHING + 自己平移,在 EMUI/HarmonyOS 上
* 既拿不到正确的键盘高度,Dialog 本身也不被推动,输入框会被键盘盖住。
*
* 现在双保险:
* 1. 标准路径靠 ADJUST_RESIZE 收缩满屏容器,贴底内容自动抬到键盘之上;
* 2. 某些 ROM 忽略 resize 时,按可见显示区域与容器高度的差值兜底平移。
*/
fun fitOnIme(content: View) {
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)
val root = content.rootView
imeRoot = root
val threshold = (context.resources.displayMetrics.density * 100).toInt()
val screenHeight = context.resources.displayMetrics.heightPixels
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()
val listener = object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
val frame = Rect()
root.getWindowVisibleDisplayFrame(frame)
val keyboard = screenHeight - frame.bottom
container.getWindowVisibleDisplayFrame(frame)
// resize 生效时容器已经收缩,差值为 0;未生效时差值就是键盘(含导航栏)高度
val keyboard = container.height - frame.bottom
content.translationY = if (keyboard > threshold) -keyboard.toFloat() else 0f
}
}
root.viewTreeObserver.addOnGlobalLayoutListener(listener)
container.viewTreeObserver.addOnGlobalLayoutListener(listener)
imeContainer = container
imeListener = listener
}
override fun dismiss() {
imeListener?.let { imeRoot?.viewTreeObserver?.removeOnGlobalLayoutListener(it) }
imeListener?.let { imeContainer?.viewTreeObserver?.removeOnGlobalLayoutListener(it) }
imeListener = null
imeRoot = null
imeContainer = null
super.dismiss()
}
override fun show() {
super.show()
window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
if (fullHeight) ViewGroup.LayoutParams.MATCH_PARENT else ViewGroup.LayoutParams.WRAP_CONTENT,
)
}
}