监听键盘弹出的方法总结(两个)

   private View activityRootView;
    //屏幕高度
    private int screenHeight = 0;
    //软件盘弹起后所占高度阀值
    private int keyHeight = 0;


    @Override
    protected void onDestroy() {
        super.onDestroy();
      //  rootView.getViewTreeObserver().removeGlobalOnLayoutListener(layoutListener);
    }
    /**
     * 添加软键盘状态监听器
     */
    protected void setKeyboardStateListener() {

        rootView = getWindow().getDecorView().findViewById(android.R.id.content);
     //   rootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        //获取屏幕高度
        screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
        //阀值设置为屏幕高度的1/3
        keyHeight = screenHeight/3;
        spring.setCurrentValue(0);
        spring.addListener(new SimpleSpringListener() {

            @Override
            public void onSpringUpdate(Spring spring) {
                // You can observe the updates in the spring
                // state by asking its current value in onSpringUpdate.
                float value = (float) spring.getCurrentValue();
                float scale = 1f - (value * 0.5f);
                Log.i("scale", scale + "");
                iv_icon.setScaleX(scale);
                iv_icon.setScaleY(scale);
            }

        });
        rootView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){

                    Toast.makeText(Login_withTitleTC.this, "监听到软键盘弹起...", Toast.LENGTH_SHORT).show();

                    spring.setEndValue(0.5);
                    ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", 130.0f);
                    animator.setDuration(200);
                    animator.start();
                    //按钮上移
                    ObjectAnimator animator1 = ObjectAnimator.ofFloat(tvNext, "translationY", -30.0f);
                    animator1.setDuration(200);
                    animator1.start();

                }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){
                    spring.setEndValue(0);
                    ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", -0f);
                    animator.setDuration(200);
                    animator.start();
                    //按钮下移
                    ObjectAnimator animator1 = ObjectAnimator.ofFloat(tvNext, "translationY", 0f);
                    animator1.setDuration(200);
                    animator1.start();
                    Toast.makeText(Login_withTitleTC.this, "监听到软件盘关闭...", Toast.LENGTH_SHORT).show();

                }
            }
        });
    }

    private ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            rootView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (isKeyboardShown(rootView)) {
                        //  ToastUtil.showToast(Login_withTitleTC.this, "键盘弹出");
                        spring.setCurrentValue(0);
                        spring.addListener(new SimpleSpringListener() {

                            @Override
                            public void onSpringUpdate(Spring spring) {
                                // You can observe the updates in the spring
                                // state by asking its current value in onSpringUpdate.
                                float value = (float) spring.getCurrentValue();
                                float scale = 1f - (value * 0.5f);
                                Log.i("scale", scale + "");
                                iv_icon.setScaleX(scale);
                                iv_icon.setScaleY(scale);
                            }

                        });
                        spring.setEndValue(0.5);
                        ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", 130.0f);
                        animator.setDuration(200);
                        animator.start();
                        //按钮上移
                        ObjectAnimator animator1 = ObjectAnimator.ofFloat(tvNext, "translationY", -30.0f);
                        animator1.setDuration(200);
                        animator1.start();

                    } else {
                        //  ToastUtil.showToast(Login_withTitleTC.this, "键盘隐藏");

                        spring.setEndValue(0);

//                            int childCount = constraintLayout.getChildCount();
//                            for (int i = 0; i < childCount; i++) {
//                                final View view = constraintLayout.getChildAt(i);
//
//                                springChain.addSpring(new SimpleSpringListener() {
//                                    @Override
//                                    public void onSpringUpdate(Spring spring) {
//                                        view.setTranslationY((float) spring.getCurrentValue());
//                                    }
//                                });
//                            }
//
//                            List<Spring> springs = springChain.getAllSprings();
//                            for (int i = 0; i < springs.size(); i++) {
//                                springs.get(i).setCurrentValue(1080);
//                            }
//
//                            springChain.setControlSpringIndex(0).getControlSpring().setEndValue(0);

                        ObjectAnimator animator = ObjectAnimator.ofFloat(constraintLayout, "translationY", 0f);
                        animator.setDuration(200);
                        animator.start();
                        //按钮下移
                        ObjectAnimator animator1 = ObjectAnimator.ofFloat(tvNext, "translationY", 0f);
                        animator1.setDuration(200);
                        animator1.start();


                    }
                }
            }, 100);

        }
    };

    /**
     * 判断软键盘是否打开
     *
     * @param rootView 最上层布局
     * @return 打开:true,隐藏:false
     */
    private boolean isKeyboardShown(View rootView) {
        //获取当前屏幕内容的高度
        int screenHeight = getWindow().getDecorView().getHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

        return screenHeight - rect.bottom != 0;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值