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

你可能感兴趣的:(Android 键盘弹起view在其上面, 背景挤压上移问题, 点击空白区域不消失问题)