android 监听输入法弹出跟隐藏

第一种方法:

   这种方法兼容了有虚拟按键手机.

rlAll.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (rlAll.getHeight() == 0) {
            return;
        }
        if(isCome){
            //在布局完成后再执行判断.
            //获取当前界面高度跟开始的布局高度比较(有虚拟键的手机要判断一下(navigationBarHeight虚拟按键的高度,因为当用户把虚拟按键弹出会触发布局会判断小于而混乱.))
            if((mTypewrit -scrollView.getHeight())> navigationBarHeight){
                Log.e("aaa", "onGlobalLayout: "+"输入法弹出了");
                btnNext.setVisibility(View.GONE);
            }else{
                Log.e("aaa", "onGlobalLayout: "+"输入法隐藏了");
                btnNext.setVisibility(View.VISIBLE);
            }
        }
        if (!isCome) {
            //这里获取布局完成后界面的高度,(注意值能控制方法执行一次,不然Typewrit会重复赋值,无法判断)
            isCome = true;
            mTypewrit =rlAll.getHeight();
        }
    }
});

当然还有虚拟按键的获取:

public class FictitiousUtils {
    //获取虚拟按键的高度
    public static int getNavigationBarHeight(Context context) {
        int result = 0;
        if (hasfictitious(context)) {
            Resources res = context.getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

    /**
     * 检查是否存在虚拟按键栏
     *
     * @param context
     * @return
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static boolean hasfictitious(Context context) {
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");
        if (resourceId != 0) {
            boolean hasNav = res.getBoolean(resourceId);
            // check override flag
            String sNavBarOverride = getNavBarOverride();
            if ("1".equals(sNavBarOverride)) {
                hasNav = false;
            } else if ("0".equals(sNavBarOverride)) {
                hasNav = true;
            }
            return hasNav;
        } else { // fallback
            return !ViewConfiguration.get(context).hasPermanentMenuKey();
        }
    }

    /**
     * 判断虚拟按键栏是否重写
     *
     * @return
     */
    private static String getNavBarOverride() {
        String sNavBarOverride = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable e) {
            }
        }
        return sNavBarOverride;
    }
}

第二种方法:

 这个方法存在问题.就不多说了


  /*  Rect rect = new Rect();
                rlAll.getWindowVisibleDisplayFrame(rect);
                int rootInvisibleHeight = rlAll.getRootView().getHeight() - rect.bottom;
                if (rootInvisibleHeight <= 100) {
                    //软键盘隐藏啦
                    btnNext.setVisibility(View.VISIBLE);
                } else {
                    软键盘弹出啦
                    btnNext.setVisibility(View.GONE);
                }*/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值