使用的是一个开源库KeyboardVisibilityEvent
KeyboardVisibilityEvent的使用:
1:
1.1 :
需依赖mavenCentral()库
1.2 :
dependencies {
implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:3.0.0-RC3'
}
2:
在Fragment中的使用实例:(手动销毁)
private var unregistrar : Unregistrar? = null
override fun onResume() {
super.onResume()
unregistrar = registerEventListener(
activity,
KeyboardVisibilityEventListener {
Log.e("dukerDev","键盘展开:$it")
})
}
override fun onPause() {
super.onPause()
unregistrar?.unregister()
}
在Activity中使用:(自动销毁)
KeyboardVisibilityEvent.setEventListener(
getActivity(),
new KeyboardVisibilityEventListener() {
@Override
public void onVisibilityChanged(boolean isOpen) {
// some code depending on keyboard visiblity status
}
});
在Fragment中使用:(自动销毁,关联自LifecycleOwner的ON_DESTROY事件)
KeyboardVisibilityEvent.setEventListener(
getActivity(),
getLifecycleOwner(),
new KeyboardVisibilityEventListener() {
@Override
public void onVisibilityChanged(boolean isOpen) {
// some code depending on keyboard visiblity status
}
});
在Fragment中使用:(手动销毁)
// get Unregistrar
Unregistrar unregistrar = KeyboardVisibilityEvent.registerEventListener(
getActivity(),
new KeyboardVisibilityEventListener() {
@Override
public void onVisibilityChanged(boolean isOpen) {
// some code depending on keyboard visiblity status
}
});
// call this method when you don't need the event listener anymore
unregistrar.unregister();
我们这一生最遗憾的事情之一,就是把我们最糟糕的一面留给了最亲近的人。 ————《这个杀手不太冷》
当你年轻时,以为什么都有答案,可是老了的时候,你可能又觉得其实人生并没有所谓的答案。 ————《堕落天使》