android 处理软键盘弹出时的一种方法

本文介绍了一种在Android应用中当软键盘弹出时,如何通过调整ScrollView的高度来保持界面元素位置不变的方法。通过监听全局布局变化,获取屏幕可见区域,并据此调整ScrollView的高度。

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

首先说明 我的思路

在查看了很多资料之后 发现了一些有用的 一些对我没用的
之后我的思路是这样的 :

先展示我的这里写图片描述
这里写图片描述

效果是 上部的内容不变 下面的ScrollView 整体上移动

思路:如果检测到软键盘弹出 就让scrollView高度缩小

额 思路看起来好简单
实现起来也是~~

我的软键盘模式

<activity android:name=".shouye.Shouye_Bianji"
            android:windowSoftInputMode="adjustResize"/>

all.为页面的整体布局的linearlayout
main 是页面滚动的ScrollView

我的大致布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#333333"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:clipToPadding="true"
    android:id="@+id/all"
    android:fitsSystemWindows="true">
    <LinearLayout
        android:id="@+id/top"
        android:layout_width="fill_parent"
        android:layout_height="43dp">
    </LinearLayout>
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main"
        android:background="#f2f2f2">
    </ScrollView>
    <requestFocus/>
</LinearLayout>

得到main 在键盘没有的时候的高度

main = (ScrollView) findViewById(R.id.main);
        main.post(new Runnable() {
            @Override
            public void run() {
                main_height = main.getHeight();
            }
        });

这是得到 all 的高度

all = (LinearLayout) findViewById(R.id.all);
        all.post(new Runnable() {
            @Override
            public void run() {
                all_height = all.getBottom();
                System.out.println("高度时--》" + all_height );
            }
        });

设置main 的高度

all.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect rect = new Rect();
                all.getWindowVisibleDisplayFrame(rect);
                int mainInvisibleHeight = all_height - rect.bottom;
//这是得到状态栏高度的一种方法
                int result = 0;
                int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    result = getResources().getDimensionPixelSize(resourceId);
                }
                if (mainInvisibleHeight > 100) {
                    ViewGroup.LayoutParams layoutParams = main.getLayoutParams();
                    layoutParams.height = mainInvisibleHeight - top_height + result * 2;
                    System.out.println(result + " " + top_height + " " + all_height + " " + rect.bottom);
                    main.setLayoutParams(layoutParams);
                } else {
                    ViewGroup.LayoutParams layoutParams = main.getLayoutParams();
                    layoutParams.height = main_height;
                    main.setLayoutParams(layoutParams);
                }
            }
        });

如果你看到了下面,感谢,不过因为我一直认为思路比较重要,所以我一般不会写demo,而且这段时间比较乱,如果有时间,我一定坐下来写出直接可以调用的小框架,思路就是这样,当然,如果我忘了,也就忘了……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值