Android:执行期间出现WindowInsetsAnimationCallback错误

问题描述

我想知道软键盘的动画关闭何时完成。所以我尝试使用这个功能

val rootLayout = findViewById<ConstraintLayout>(R.id.root_layout)

rootLayout.setwindowInsetsAnimationCallback(object : WindowInsetsAnimation.Callback(disPATCH_MODE_STOP) {
            override fun onProgress(insets: WindowInsets,runningAnimations: MutableList<WindowInsetsAnimation>): WindowInsets {
                //Todo("Not yet implemented")
                return insets
            }

            override fun onEnd(animation: WindowInsetsAnimation) {
                super.onEnd(animation)
                val showingKeyboard = rootLayout.rootwindowInsets.isVisible(WindowInsets.Type.ime())
                if (!showingKeyboard) {
                    println("Keyboard is Now closed")
                }
            }
        })
        

但是当我在手机(和几个 emu)上启动应用程序时,我不得不面对这个错误

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/WindowInsetsAnimation$Callback;
        at java.lang.class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1251)
        at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:3222)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3459)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2046)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:225)
        at android.app.ActivityThread.main(ActivityThread.java:7564)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: java.lang.classNotFoundException: Didn't find class "android.view.WindowInsetsAnimation$Callback" on path: DexPathList[[zip file "/data/app/com.example.buginsetsanimationlistener-wRvK7cMTAxdBF1RCG19tlA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.buginsetsanimationlistener-wRvK7cMTAxdBF1RCG19tlA==/lib/arm64,/system/lib64,/system/product/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:230)
        at java.lang.classLoader.loadClass(ClassLoader.java:379)
        at java.lang.classLoader.loadClass(ClassLoader.java:312)
        at java.lang.class.newInstance(Native Method) 
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) 
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45) 
        at android.app.Instrumentation.newActivity(Instrumentation.java:1251) 
        at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:3222) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3459) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2046) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:225) 
        at android.app.ActivityThread.main(ActivityThread.java:7564) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 

我不明白我该怎么做,谢谢你的回答!

解决方法

我终于找到了我的秘密。此方法适用于 API 30+。但我找到了另一种方法来实现我的目标: 当我自己关闭软键盘时,这是有效的。

imm.hideSoftInputFromWindow(rootLayout.windowToken,object : ResultReceiver(null) {
        override fun onReceiveResult(resultCode: Int,resultData: Bundle?) {
            if (resultCode == InputMethodManager.RESULT_UNCHANGED_HIDDEN ||
                resultCode == InputMethodManager.RESULT_HIDDEN) {
                //Soft has finished the animation
                }
            }
        }
    })

希望这个答案对某人有所帮助:)