解决 侵入式下 EditText位于屏幕底部时会被软键盘覆盖的问题

转自:http://blog.csdn.net/huangxiaoguo1/article/details/53081229?locationNum=3&fps=1

源码地址:http://download.csdn.net/detail/guojiel/9587855

解决 侵入式下  EditText位于屏幕底部时会被软键盘覆盖的问题

全屏模式下,即使将activity的windowSoftInputMode的属性设置为:adjustResize,在键盘显示时它未将Activity的Screen向上推动

感谢Ricardo提供的轮子,他在stackoverflow找到了解决方案。有人已经封装好了该类,你只需引用就OK了。

使用方法
在你的Activity的oncreate()方法里调用AndroidBug5497Workaround.assistActivity(this);即可。
注意:在setContentView(R.layout.xxx)之后调用。

在AndroidManifest.xml

<!-- 聊天页面 -->
        <activity
            android:name=".ui.main.ChatActivity"
            android:launchMode="singleTask"
            android:screenOrientation="portrait"
            android:theme="@style/horizontal_slide"
            android:windowSoftInputMode="adjustResize" />

在AndroidBug5497Workaround 中

package cn.hnshangyu.officeautomationsystem.utils;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;

public class AndroidBug5497Workaround {


    public static void assistActivity (Activity activity) {
        new AndroidBug5497Workaround(activity);
    }

    private View mChildOfContent;
    private int usableHeightPrevious;
    private FrameLayout.LayoutParams frameLayoutParams;

    private AndroidBug5497Workaround(Activity activity) {
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
        frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard/4)) {
                // keyboard probably just became visible
                frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
            } else {
                // keyboard probably just became hidden
                frameLayoutParams.height = usableHeightSansKeyboard;
            }
            mChildOfContent.requestLayout();
            usableHeightPrevious = usableHeightNow;
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);
        return (r.bottom - r.top);
    }

}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当 RecyclerView 中的 EditText 获取焦点并弹起软键盘时,可能会出现软键盘遮挡了 EditText 的情况,这个问题可以通过以下几种方解决: 1. 使用 adjustResize 属性 在 Activity 的 AndroidManifest.xml 文件中,将 Activity 的 windowSoftInputMode 属性设置为 adjustResize,即可在软键盘弹出时自动调整布局,将 EditText 上移以避免被遮挡。 ```xml <activity android:name=".YourActivity" android:windowSoftInputMode="adjustResize"> ... </activity> ``` 2. 使用 android:fitsSystemWindows 属性 在 RecyclerView 的父布局中添加 android:fitsSystemWindows="true" 属性,这样可以让 RecyclerView 的布局留出足够的空间来显示软键盘,避免 EditText 被遮挡。 ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> ``` 3. 使用 scrollToPosition 或 scrollToPositionWithOffset 方法 在 RecyclerView 中的 EditText 获取焦点时,手动将 RecyclerView 滚动到 EditText 所在的位置,这样就可以避免 EditText 被遮挡。 ```java // 获取 EditText 在 RecyclerView 中的位置 int position = mAdapter.getPosition(editText); // 滚动 RecyclerView 到指定位置 mRecyclerView.scrollToPosition(position); // 或者使用 scrollToPositionWithOffset 方法,更精确地定位到 EditText 的位置 int offset = 100; // 偏移量,可根据实际情况调整 mLayoutManager.scrollToPositionWithOffset(position, offset); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值