Nestscrollview EditText 点击弹出下拉菜单被弹出软键盘遮挡

文章讲述了如何在Android应用中处理NestedScrollView内的EditText在软键盘弹出时被遮挡的问题,通过监听键盘高度变化并滚动视图来避免遮挡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nestscrollview EditText 点击弹出下拉菜单被弹出软键盘遮挡


找了一圈没有适合自己的,谈思路 网上很多 但是不一定适合自己 要思考自己的问题点
1.Mainfest android:windowSoftInputMode=“adjustPan”
1.监听软键盘弹出

   private void restoreEditText() {
        NestedScrollView scrollView = findViewById(R.id.nestedScrollView); // 根据实际情况获取 ScrollView 或 NestedScrollView 对象
        int scrollY = (int) etSysName.getY(); // 获取 EditText 在布局中的相对位置
        scrollView.smoothScrollTo(0, -scrollY); // 滚动到 EditText 的位置
    }

    private void scrollToEditText() {
        NestedScrollView scrollView = findViewById(R.id.nestedScrollView); // 根据实际情况获取 ScrollView 或 NestedScrollView 对象
        int scrollY = (int) etSysName.getY(); // 获取 EditText 在布局中的相对位置
        scrollView.smoothScrollTo(0, scrollY); // 滚动到 EditText 的位置
    }


    // 解决系统名称软键盘弹出下拉菜单被遮挡问题
private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener;
    private void adjustSysDropDown(){
        keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
            private int previousHeight = 0;

            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                etSysName.getWindowVisibleDisplayFrame(r);
                int screenHeight = etSysName.getRootView().getHeight();
                int keypadHeight = screenHeight - r.bottom;

                if (keypadHeight > screenHeight * 0.15) {
                    // 软键盘显示
                    if (keypadHeight != previousHeight) {
                        previousHeight = keypadHeight;
                        // 执行相应的操作,比如滚动 EditText 或调整布局
                        scrollToEditText();
                    }
                } else {
                    // 软键盘隐藏
                    if (previousHeight != 0) {
                        previousHeight = 0;
                        // 执行相应的操作,比如将 EditText 恢复到原始位置
//                        restoreEditText();
                    }
                }
            }
        };

        etSysName.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);
    }
```



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值