Android 监听键盘关闭

直接上代码:
1.自定义View

public class KeyboardLayout extends RelativeLayout {
    private static final String TAG = KeyboardLayout.class.getSimpleName();
    public static final byte KEYBOARD_STATE_SHOW = -3;
    public static final byte KEYBOARD_STATE_HIDE = -2;
    public static final byte KEYBOARD_STATE_INIT = -1;
    public boolean mHasInit;
    private boolean mHasKeybord;
    private int mHeight;
    private onKybdsChangeListener mListener;

    public void setmHasInit(boolean mHasInit) {
        this.mHasInit = mHasInit;
    }

    public void setmHasKeybord(boolean mHasKeybord) {
        this.mHasKeybord = mHasKeybord;
    }

    public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public KeyboardLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public KeyboardLayout(Context context) {
        super(context);
    }

    /**
     * set keyboard state listener
     */
    public void setOnkbdStateListener(onKybdsChangeListener listener) {
        mListener = listener;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (!mHasInit) {
            mHasInit = true;
            mHeight = b;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
            }
        } else {
            mHeight = mHeight < b ? b : mHeight;
        }
        if (mHasInit && mHeight > b) {
            mHasKeybord = true;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
            }
            Log.w(TAG, "show keyboard.......");
        }
        if (mHasInit && mHasKeybord && mHeight == b) {
            mHasKeybord = false;
            if (mListener != null) {
                mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
            }
            Log.w(TAG, "hide keyboard.......");
        }
    }

    public interface onKybdsChangeListener {
        public void onKeyBoardStateChange(int state);
    }
}

2.xml布局
 

<?xml version="1.0" encoding="utf-8"?>
<com.qyjs.main.views.comment.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl_outside_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <RelativeLayout
            android:id="@+id/rl_background"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/llInputView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/main_comment_input_bg_night"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                android:orientation="horizontal">

                <EditText
                    android:id="@+id/et_input_message"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginRight="9dp"
                    android:layout_marginBottom="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/main_round_input_bg_night"
                    android:gravity="center_vertical"
                    android:hint="喜欢就要大声说出"
                    android:imeOptions="flagNoExtractUi"
                    android:lineSpacingMultiplier="1.2"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:maxLength="80"
                    android:maxLines="7"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:scrollbars="vertical"
                    android:textSize="15sp"
                    tools:ignore="InvalidImeActionId,MissingConstraints" />

              
            </LinearLayout>


        </RelativeLayout>

    </androidx.core.widget.NestedScrollView>

</com.qyjs.main.views.comment.KeyboardLayout>

3.设置监听:
 

setOnkbdStateListener(new KeyboardLayout.onKybdsChangeListener() {
            @Override
            public void onKeyBoardStateChange(int state) {
                switch (state) {
                    case KeyboardLayout.KEYBOARD_STATE_HIDE:
                        //    Toast.makeText(getApplicationContext(), "软键盘隐藏", Toast.LENGTH_SHORT).show();
                        //   Log.e("KeyboardLayout","KeyboardLayout----软键盘隐藏");
                        mOnTextSendListener.onInputTextString(messageTextView.getText().toString().trim());
                        messageTextView.setText("");
                        if (isShowing()) {
                            dismiss();
                        }
                        break;
                    case KeyboardLayout.KEYBOARD_STATE_SHOW:
                        //   Toast.makeText(getApplicationContext(), "软键盘弹起", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

4.设置弹出软键盘
 

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值