public class MyActivity extends AppCompatActivity {
/**
* 当前界面中的软件盘的状态
*/
private boolean isKeyBoardOpen = false;
/**
* 监听软键盘状态
*/
private void addOnSoftKeyBoardVisibleListener() {
final View decorView = getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
isKeyBoardOpen = (double) (rect.bottom - rect.top) / decorView.getHeight() < 0.8;
}
});
}
}