demo效果
keyboardLayout.gif
GitHub上的源碼
介紹
KeyboardLayout 用于監(jiān)聽軟鍵盤的彈起和隱藏。
使用
KeyboardLayout 使用簡單,只需將其加入到布局文件即可互墓,無其他使用限制,KeyboardLayout只是充當一個布局監(jiān)聽器的作用蒋搜,即可實現(xiàn)對整個布局樹進行監(jiān)聽篡撵。
對于Android Studio用戶,可添加:
compile 'com.hz.android.keyboardlayout:library:1.0'
- 布局文件
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
......
<com.hz.android.keyboardlayout.KeyboardLayout
android:id="@+id/keyboard_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
......
</android.support.constraint.ConstraintLayout
- 設置監(jiān)聽
KeyboardLayout keyboardLayout = (KeyboardLayout) findViewById(R.id.keyboard_layout);
keyboardLayout.setKeyboardLayoutListener(new KeyboardLayout.KeyboardLayoutListener() {
@Override
public void onKeyboardStateChanged(boolean isActive, int keyboardHeight) {
if (isActive) { //軟鍵盤彈起
//do something
} else { //軟鍵盤隱藏
//do something
}
}
});