1
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"minSdkVersion": "21"
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
|
||||
import keyboardHeightChangeByKt from 'uts.sdk.modules.utsXKeyboardheightchangeS.keyboardHeightChangeByKt';
|
||||
|
||||
|
||||
// 监听键盘高度变化
|
||||
@UTSJS.keepAlive
|
||||
export const keyboardHeightChange = (callback : (h : number) => void):()=>void => {
|
||||
const cancelFun = keyboardHeightChangeByKt(UTSAndroid.getUniActivity()!,(keyboardHeight:Int)=>{
|
||||
callback(UTSAndroid.devicePX2px(keyboardHeight))
|
||||
});
|
||||
const cancel = ()=>{
|
||||
cancelFun()
|
||||
};
|
||||
return cancel;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package uts.sdk.modules.utsXKeyboardheightchangeS
|
||||
import android.app.Activity
|
||||
import android.graphics.Rect
|
||||
import android.os.Build
|
||||
import android.view.View
|
||||
import android.view.ViewTreeObserver
|
||||
import android.view.WindowInsets
|
||||
|
||||
typealias cacelCallBack = () -> Unit
|
||||
|
||||
fun keyboardHeightChangeByKt(activity: Activity, callback: (height: Int) -> Unit): cacelCallBack {
|
||||
val rootView = activity.window.decorView.rootView
|
||||
val rect = Rect()
|
||||
var lastHeight = 0
|
||||
|
||||
val listener = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
// Android 11及以上使用WindowInsets API
|
||||
View.OnApplyWindowInsetsListener { _, insets ->
|
||||
val imeHeight = insets.getInsets(WindowInsets.Type.ime()).bottom
|
||||
val navigationBarHeight = insets.getInsets(WindowInsets.Type.navigationBars()).bottom
|
||||
val keyboardHeight = if (imeHeight > 0) imeHeight - navigationBarHeight else 0
|
||||
|
||||
if (keyboardHeight != lastHeight) {
|
||||
lastHeight = keyboardHeight
|
||||
callback(keyboardHeight)
|
||||
}
|
||||
insets
|
||||
}
|
||||
} else {
|
||||
// Android 5.0 - Android 10使用传统方法
|
||||
ViewTreeObserver.OnGlobalLayoutListener {
|
||||
rootView.getWindowVisibleDisplayFrame(rect)
|
||||
val screenHeight = rootView.height
|
||||
val keyboardHeight = screenHeight - rect.bottom
|
||||
|
||||
// 考虑导航栏高度
|
||||
val navigationBarHeight = getNavigationBarHeight(activity)
|
||||
|
||||
val actualKeyboardHeight = if (keyboardHeight > navigationBarHeight) {
|
||||
keyboardHeight - navigationBarHeight
|
||||
} else 0
|
||||
|
||||
if (actualKeyboardHeight != lastHeight) {
|
||||
lastHeight = actualKeyboardHeight
|
||||
callback(actualKeyboardHeight)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
rootView.setOnApplyWindowInsetsListener(listener as View.OnApplyWindowInsetsListener)
|
||||
} else {
|
||||
rootView.viewTreeObserver.addOnGlobalLayoutListener(listener as ViewTreeObserver.OnGlobalLayoutListener)
|
||||
}
|
||||
|
||||
return {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
rootView.setOnApplyWindowInsetsListener(null)
|
||||
} else {
|
||||
rootView.viewTreeObserver.removeOnGlobalLayoutListener(listener as ViewTreeObserver.OnGlobalLayoutListener)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 添加原生方法检测导航栏高度
|
||||
private fun getNavigationBarHeight(activity: Activity): Int {
|
||||
val resources = activity.resources
|
||||
val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
|
||||
return if (resourceId > 0 && hasNavigationBar(activity)) {
|
||||
resources.getDimensionPixelSize(resourceId)
|
||||
} else 0
|
||||
}
|
||||
|
||||
// 检测设备是否有导航栏
|
||||
private fun hasNavigationBar(activity: Activity): Boolean {
|
||||
val windowManager = activity.windowManager
|
||||
val d = windowManager.defaultDisplay
|
||||
|
||||
val realDisplayMetrics = android.util.DisplayMetrics()
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
d.getRealMetrics(realDisplayMetrics)
|
||||
}
|
||||
|
||||
val displayMetrics = android.util.DisplayMetrics()
|
||||
d.getMetrics(displayMetrics)
|
||||
|
||||
return realDisplayMetrics.heightPixels > displayMetrics.heightPixels
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"deploymentTarget": "12"
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { NotificationCenter, OperationQueue, Notification } from "Foundation";
|
||||
import { UIResponder } from "UIKit";
|
||||
import { CGRect } from "CoreFoundation";
|
||||
|
||||
// 监听键盘高度变化
|
||||
@UTSJS.keepAlive
|
||||
export function keyboardHeightChange(callback : (h : number) => void):()=>void {
|
||||
let lastHeight = 0
|
||||
const showObserver = NotificationCenter.default.addObserver(
|
||||
forName = UIResponder.keyboardWillShowNotification,
|
||||
object= null,
|
||||
queue= OperationQueue.main,
|
||||
using = (notification:Notification)=>{
|
||||
if(notification==null) return;
|
||||
let keyboardFrame = notification.userInfo?.get(UIResponder.keyboardFrameEndUserInfoKey) as CGRect|null
|
||||
if(keyboardFrame==null) return;
|
||||
const keyGrect = keyboardFrame! as CGRect;
|
||||
let keyboardheight = keyGrect.height
|
||||
|
||||
// 计算实际键盘高度(减去安全区域)
|
||||
let actualKeyboardHeight = Math.max(0, Number(keyboardheight) - uni.getWindowInfo().safeAreaInsets.bottom)
|
||||
// 只有当高度变化时才回调
|
||||
if(actualKeyboardHeight != lastHeight) {
|
||||
lastHeight = actualKeyboardHeight
|
||||
DispatchQueue.main.async(execute = () : void => {
|
||||
callback(actualKeyboardHeight)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
)
|
||||
|
||||
const hideObserver = NotificationCenter.default.addObserver(
|
||||
forName = UIResponder.keyboardWillHideNotification,
|
||||
object= null,
|
||||
queue= OperationQueue.main,
|
||||
using = (_:Foundation.Notification)=>{
|
||||
if(lastHeight != 0) {
|
||||
lastHeight = 0
|
||||
DispatchQueue.main.async(execute = () : void => {
|
||||
callback(0)
|
||||
})
|
||||
}
|
||||
}
|
||||
)
|
||||
const cancel = ()=>{
|
||||
NotificationCenter.default.removeObserver(showObserver)
|
||||
NotificationCenter.default.removeObserver(hideObserver)
|
||||
};
|
||||
return cancel
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
type cacelCall = () => void;
|
||||
export const keyboardHeightChange = (callback : (h : number) => void) : cacelCall => {
|
||||
const fun = (result) => {
|
||||
callback(result.height)
|
||||
}
|
||||
wx.onKeyboardHeightChange(fun)
|
||||
return () => {
|
||||
wx.offKeyboardHeightChange(fun)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
type cacelCall = () => void;
|
||||
export const keyboardHeightChange = (callback : (h : number) => void) : cacelCall => {
|
||||
|
||||
|
||||
|
||||
return () => {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user