软键盘弹起后顶起EditText的完美解决方案

在网上看了很多种顶起EditText的方案,发现都不能达到我的要求,所以自己想了个办法完美解决这个问题。

解决思路如下:先监听到软键盘的显示和隐藏,再通过计算软键盘的高度,来控制事先在EditView布局下的View的高度,最后实现一个顶起效果

第一步:监听软键盘的弹出隐藏和获取高度,这里用了一个辅助监听类


import android.app.Activity;
import android.graphics.Rect;
import android.view.View;

/**
 * 监听软键盘的弹出和隐藏
 */
public class SoftKeyBoardListener {

    private View rootView;//activity的根视图
    int rootViewVisibleHeight;//纪录根视图的显示高度
    private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;

    public SoftKeyBoardListener(Activity activity) {
        //获取activity的根视图
        rootView = activity.getWindow().getDecorView();

        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
            //获取当前根视图在屏幕上显示的大小
            Rect r = new Rect();
            rootView.getWindowVisibleDisplayFrame(r);

            int visibleHeight = r.height();
            System.out.println(""+visibleHeight);
            if (rootViewVisibleHeight == 0) {
                rootViewVisibleHeight = visibleHeight;
                return;
            }

            //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
            if (rootViewVisibleHeight == visibleHeight) {
                return;
            }

            //根视图显示高度变小超过200,可以看作软键盘显示了
            if (rootViewVisibleHeight - visibleHeight > 200) {
                if (onSoftKeyBoardChangeListener != null) {
                    onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
                }
                rootViewVisibleHeight = visibleHeight;
                return;
            }

            //根视图显示高度变大超过200,可以看作软键盘隐藏了
            if (visibleHeight - rootViewVisibleHeight > 200) {
                if (onSoftKeyBoardChangeListener != null) {
                    onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
                }
                rootViewVisibleHeight = visibleHeight;
                return;
            }

        });
    }

    private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
    }

    public interface OnSoftKeyBoardChangeListener {
        void keyBoardShow(int height);

        void keyBoardHide(int height);
    }

    public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
        softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
    }
}

第二步:在Activity中实现这个接口,并动态给EditText下面的View设置高度

/**
     * 使键盘能够弹起
     */
    private void initEditHight() {
        //监听软键盘弹出,并获取软键盘高度
        SoftKeyBoardListener.setListener(this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
            //软键盘弹起事件  并给View设置高度
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view_keybord.getLayoutParams();
                layoutParams.height = height;
                view_keybord.setLayoutParams(layoutParams);
            }

            @Override
            public void keyBoardHide(int height) {
             //软键盘隐藏事件  并给View设置高度为0
                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view_keybord.getLayoutParams();
                layoutParams.height = 0;
                view_keybord.setLayoutParams(layoutParams);
            }
        });

简单的,我们就完成了整个功能,以下是xml

<LinearLayout
        android:id="@+id/ll_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:elevation="3dp"
        android:orientation="horizontal"
        android:paddingLeft="15dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:paddingRight="15dp">


        <EditText
            android:id="@+id/et_im"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/bg_gray_broke"
            android:gravity="center_vertical"
            android:hint="聊点什么吧"
            android:paddingLeft="10dp"
            android:paddingTop="7dp"
            android:paddingRight="10dp"
            android:paddingBottom="7dp"
            android:singleLine="true"
            android:textColor="@color/text_blackA6"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/tv_send"
            android:text="发送"
            android:textColor="@color/red_fe8977"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:gravity="center"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp" />

    </LinearLayout>

    <View
        android:id="@+id/view_keybord"
        android:layout_width="match_parent"
        android:layout_height="0dp">

    </View>

大功告成,如果有受到启发和帮助,点个赞吧

你可能感兴趣的:(Android,项目实用)