Android:完美解决 — 表情键盘与输入法键盘显示冲突方案

本文介绍了一种在输入法键盘隐藏后才显示表情键盘的方法,以改善用户体验。通过监听布局视图高度变化并利用一个高度为0的View来判断输入法是否完全隐藏。

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

在做发帖功能的时候,一般都会用到表情,当在输入法键盘显示的情况下,点击显示表情键盘的按钮,如果只是简单的setVisibility,同时隐藏输入法,这时表情键盘会被往上弹出,这个效果就让使用体验不太友好。


我的解决方案是:在输入法键盘完全隐藏后,再将表情键盘显示。

1、在xml布局中的最底部加一个View,高度为0,用来在代码中判断该view的y坐标是否大于或等于屏幕的高度,如果是,这表明输入法完全隐藏;

2、通过mainLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener 来监听整个布局的视图高度,判断输入法是否完全隐藏就在这里面操作。


下面代码不太完整,只是用来大概展示思路:

xml布局

<?xml version="1.0" encoding="utf-8"?>
<com.hmy.view.ResizeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">


    <FrameLayout
        android:id="@+id/content_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/head_layout">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <View
                android:id="@+id/bottom"
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:layout_alignParentBottom="true" />

            <!-- 编辑框 -->  
            <EditText                
                android:id="@+id/titleEditText"
                android:layout_width="match_parent"
		android:layout_height="match_parent"
		android:layout_marginTop="1.5dp"></EditText>

        </RelativeLayout>

        <LinearLayout
            android:id="@+id/bottom_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#F1F1F1"
            android:orientation="vertical">

            <!-- 底部功能条,表情按钮在这里面 -->
            <include layout="@layout/view_toolbtn" />

            <!-- 表情键盘,默认为gone -->
            <include layout="@layout/view_emoji" />

            <!-- 这个View就是用来标记输入法位置的 -->
            <View
                android:id="@+id/keyboard_place_view"
                android:layout_width="match_parent"
                android:layout_height="0dp" />

        </LinearLayout>
    </FrameLayout>

</com.hmy.view.ResizeLayout>

注:如果不知道ResizeLayout是什么,百度一下吧。




下面是核心代码,代码不完整,只提供思路:

    private ResizeLayout mainLinearLayout;
    private View mKeyBoardPlaceView;

    mainLinearLayout = (ResizeLayout) findViewById(R.id.mainLinearLayout);
    mKeyBoardPlaceView = findViewById(R.id.keyboard_place_view);



    private void setKeyBoardListener() {
        mainLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (mIsShowExpression) { // mIsShowExpression 在表情按钮的onClick事件中切换true或false
                    int[] location = new int[2];
                    mKeyBoardPlaceView.getLocationOnScreen(location);
                    // mScreenHeight 是屏幕高度,在这之前获取
                    if (location[1] >= mScreenHeight) {
                        // linearLayoutExpression 是表情键盘
                        linearLayoutExpression.setVisibility(View.VISIBLE);
                        return;
                    }
                }
            }
        });
    }

好啦,就是这么多。如果你有更好的方式,望赐教。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值