Android 键盘弹起view在其上面, 背景挤压上移问题, 点击空白区域不消失问题

  1. 设置 acitivty 的 windowSoftInputMode 为 adjustResize|stateHidden

  2. 不要设置 activity 全屏否则设置啥模式都白扯nnd,就?这货 window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

  3. 如果界面设置 background 无论是给根view设置还是单独一个view设置, 键盘弹起背景会挤压闪一下并上移. 目前解决办法在代码中设置: getWindow().getDecorView().setBackgroundResource(R.drawable.xx)

  4. 键盘弹起点击其他区域键盘不消失问题

override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                DeviceUtils.hideKeyboard(this) //隐藏键盘的代码
            }
        }
        return super.onTouchEvent(event)
    }
复制代码
  1. 键盘弹起指定view在键盘上面 kotlin code 如:要弹起的view为 EdtiText mEtContent;
rootView!!.getViewTreeObserver().addOnGlobalLayoutListener {
            val rect = Rect()
            rootView?.getWindowVisibleDisplayFrame(rect)
            val rootInvisibleHeight = ViewUtils.getDeviceHeight() - rect.bottom
            if (rootInvisibleHeight > 300) {
                val location = IntArray(2)
                mEtContent?.getLocationOnScreen(location)
                val y = location[1]
                showContentEdit(y - rect.bottom + mEtContent!!.getHeight())
            } else {
                hideContentEdit()
            }
        }
        
    private fun showContentEdit(keyboardheight: Int) {
        mEtContent?.visibility = View.VISIBLE
        val set = AnimatorSet()
        val play = set.play(ObjectAnimator.ofFloat(mEtContent, "translationY", 0f, -keyboardheight.toFloat()))
        set.duration = 200
        set.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                mEtContent?.requestFocus()
            }
        })
        set.start()
    }

    private fun hideContentEdit() {
        if (isAnimRun) {
            return
        }
        isAnimRun = true
        val set = AnimatorSet()
        val play = set.play(ObjectAnimator.ofFloat(mEtContent, "translationY", mEtContent!!.getTranslationY().toFloat(), 0f))
        set.duration = 200
        set.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                isAnimRun = false
            }
        })
        set.start()
    }
    
    public static int getDeviceHeight() {
        if (deviceHeight == 0) {
            WindowManager wm = (WindowManager) PresentationApp.inst().getApp().getSystemService(Context.WINDOW_SERVICE);
            Display d = wm.getDefaultDisplay();
            DisplayMetrics realDisplayMetrics = new DisplayMetrics();
            d.getRealMetrics(realDisplayMetrics);
            deviceHeight = realDisplayMetrics.heightPixels;
        }
        return deviceHeight;
    }
复制代码

转载于:https://juejin.im/post/5c402f9f6fb9a04a0f6574d6

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值