Android 当用户点击recyclerView时关闭键盘

Android 当用户点击recyclerView时关闭键盘,android,Android,我有一个RecyclerView,下面是一个EditText。我希望当用户在编辑文本外点击时关闭键盘 我搜索并找到了这个答案,但它对我不起作用。用户点击外部,但EditText仍有焦点 我的XML代码是: <android.support.v7.widget.RecyclerView android:id="@+id/rv_chatroom_messages_list" android:paddingBottom="@dimen/spacing_medium" an

我有一个RecyclerView,下面是一个EditText。我希望当用户在编辑文本外点击时关闭键盘

我搜索并找到了这个答案,但它对我不起作用。用户点击外部,但EditText仍有焦点

我的XML代码是:

<android.support.v7.widget.RecyclerView
    android:id="@+id/rv_chatroom_messages_list"
    android:paddingBottom="@dimen/spacing_medium"
    android:layout_weight="1"
    android:clickable="true" android:focusableInTouchMode="true" android:focusable="true"
    android:layout_width="match_parent" android:layout_height="0dp"/>

<LinearLayout
    android:orientation="horizontal"
    android:background="@color/white"
    android:layout_marginLeft="@dimen/spacing_large" android:layout_marginBottom="@dimen/spacing_medium"
    android:layout_marginRight="@dimen/spacing_large" android:paddingRight="@dimen/spacing_medium"
    android:layout_width="match_parent" android:layout_height="wrap_content">

    <EditText
        android:id="@+id/et_chatroom_send_message"
        android:layout_gravity="bottom"
        android:background="@null"
        android:textSize="@dimen/app_text_size_normal" android:hint="Type to reply"
        android:layout_marginLeft="@dimen/spacing_large" android:layout_marginRight="@dimen/spacing_large"
        android:layout_weight="1"
        android:layout_width="0dp" android:layout_height="@dimen/spacing_xlarge" android:inputType="textMultiLine" />

    <ImageView
        android:id="@+id/iv_chatroom_send_message_btn"
        android:src="@drawable/ic_pong_2"
        android:layout_gravity="center"
        android:layout_width="48dp" android:layout_height="48dp" android:contentDescription="Send msg button" />

</LinearLayout>
当editText失去焦点时关闭它

就像这样:

EditText editText = (EditText) findViewById(R.id.edittxt);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            // code to execute when EditText loses focus
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }
});
当editText失去焦点时关闭它

就像这样:

EditText editText = (EditText) findViewById(R.id.edittxt);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            // code to execute when EditText loses focus
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }
});

只需将onTouchListener设置为RecyclerView并从中调用hideKeyboard方法。

只需将onTouchListener设置为RecyclerView并从中调用hideKeyboard方法。

在RecyclerView touch listener上调用此方法

public static void hideKeyboard(final Activity activity) {
    final View view = activity.getCurrentFocus();
    final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (view == null) {
                View view2 = new View(activity);
                imm.hideSoftInputFromWindow(view2.getWindowToken(), 0);
            } else {
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }, 125);
}

在recyclerview touch listener上调用此方法

public static void hideKeyboard(final Activity activity) {
    final View view = activity.getCurrentFocus();
    final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (view == null) {
                View view2 = new View(activity);
                imm.hideSoftInputFromWindow(view2.getWindowToken(), 0);
            } else {
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }, 125);
}

这就是我正在做的-问题是,当用户点击EditText外部,点击的布局不可聚焦,然后EditText仍然有焦点…这就是我正在做的-问题是,当用户点击EditText外部,点击的布局不可聚焦,然后EditText仍然有焦点时,它似乎没有聚焦聚焦…不是针对RecyclerView,而是针对包含从发布的XML中剪切的两个视图的主视图组,并在此侦听器内检查editText.hasFocust是否为另一种方式,但如果RecyclerView覆盖editText上方的整个屏幕,则两者应实现相同的效果。但是EditText不是视图组的一部分吗/不是针对RecyclerView,而是针对包含从发布的XML中剪切的两个视图的主视图组,并在此侦听器内检查editText.hasFocust是否为另一种方式,但如果RecyclerView覆盖editText上方的整个屏幕,则两者应实现相同的功能。但是EditText不是视图组的一部分吗/