在网上搜索了很多例子感觉都不太理想
要实现键盘的弹出或关闭的首要条件是 activity的softinputmode必须是
android:windowSoftInputMode="adjustResize" 基本上所有的文章都给出了
网上我搜索到的基本都是
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
{
}
}
});
但是至少在我的手机上这个heightDiff不管键盘是否弹出都为0 而且我已经设置了
android:windowSoftInputMode="adjustResize"
在查看了很多文章后 无意中在stackoverflow上看到了别人是的实现法 思路都是一样就是判断布局移动的距离 但是判断的写法不一样 如果有人遇到和我一样 这个heightDiff为0或者不变的可以试试 我不能保证能成功 但是至少你可以试试 下面贴出别人实现的判断方法
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { private boolean wasOpened; private final int DefaultKeyboardDP = 100; // From @nathanielwolf answer... Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0); private final Rect r = new Rect(); @Override public void onGlobalLayout() { // Convert the dp to pixels. int estimatedKeyboardHeight = (int) TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, rootView.getResources().getDisplayMetrics()); // Conclude whether the keyboard is shown or not. rootView.getWindowVisibleDisplayFrame(r); int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top); boolean isShown = heightDiff >= estimatedKeyboardHeight; isSoftInputShow = isShown; if (isShown) { Log.d("Keyboard state", "Ignoring global layout change..."); Log.e(TAG, "-----------------show"); if (re_munite.getVisibility() == View.VISIBLE) { re_munite.setVisibility(View.GONE); } } else { Log.e(TAG, "------------------hide"); if (isVisibilty && re_munite.getVisibility() == View.GONE) { re_munite.setVisibility(View.VISIBLE); re_munite.bringToFront(); } } } });
这个rootView是Activity的根布局 我的事这样
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical" android:id="@+id/rootView" tools:context="com.example.pc.myapplication.SendMessageActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> <include layout="@layout/content_send_message" /> </LinearLayout>
还有别忘记了 Activity的softinputMode的设置 在你的manifest中
<activity android:name=".SendMessageActivity" android:label="@string/title_activity_send_message" android:theme="@style/AppTheme" android:windowSoftInputMode="adjustResize"> </activity>
不保证所有的能判断成功 在不能正确监听到软件盘弹出或关闭的朋友们可以试试这个方法