Android监控软键盘的开启/关闭状态

时间:2022-02-03 00:44:23
原理:监控布局尺寸的变化,来判断软键盘是否为开启或者关闭状态。

注意事项:

      1.不要将Activiy设置成FullScreen,貌似全屏不能adjustSize..

      2. AndroidManifest.xml中要设置键盘状态为ajustSize.

      如代码所示:

    

    <activity
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|screenSize|orientation"
android:screenOrientation="sensor"
android:theme="@android:style/Theme.Light.NoTitleBar"
android:windowSoftInputMode="adjustResize|stateHidden" >
</activity>

 

       监控代码:

 

                  RelativeLayout mCiRelativeLayout;
mCiRelativeLayout = (RelativeLayout) findViewById(R.id.rl_root_inMain);
mCiRelativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{

int heightDiff = mCiRelativeLayout.getRootView()
.getHeight() - mCiRelativeLayout.getHeight();

if (heightDiff > 100)
{ // 说明键盘是弹出状态

} else
{
}
}
});