监听android软键盘弹出或者关闭的方法


在网上搜索了很多例子感觉都不太理想

要实现键盘的弹出或关闭的首要条件是 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>

不保证所有的能判断成功 在不能正确监听到软件盘弹出或关闭的朋友们可以试试这个方法
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值