Android 单击“上一步”按钮后键盘关闭时,如何从编辑文本中清除焦点?

Android 单击“上一步”按钮后键盘关闭时,如何从编辑文本中清除焦点?,android,android-edittext,Android,Android Edittext,我有一个AppBarLayout,当我触摸EditText时,我会隐藏起来 SearchFragment.class binding.searchEt.setOnFocusChangeListener { view, hasFocus -> if (hasFocus) { binding.appBarLayout.setExpanded(false, true) } else { binding.appBar

我有一个
AppBarLayout
,当我触摸
EditText
时,我会隐藏起来

SearchFragment.class

 binding.searchEt.setOnFocusChangeListener { view, hasFocus ->
        if (hasFocus) {
            binding.appBarLayout.setExpanded(false, true)
        } else {
            binding.appBarLayout.setExpanded(true, true)
        }
    }
但当键盘关闭时,我无法再次显示AppBarLayout。因为当键盘关闭时,编辑文本的焦点不会改变

Xml

 <EditText
     android:id="@+id/searchEt"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_centerVertical="true"
     android:layout_marginStart="8dp"
     android:layout_toStartOf="@+id/close_button"
     android:layout_toEndOf="@+id/ic_search"
     android:background="@android:color/transparent"
     android:focusable="true"
     android:focusableInTouchMode="true"
     android:fontFamily="@font/catamaran_bold"
     android:hint="@string/search_hint"
     android:imeOptions="actionSearch"
     android:includeFontPadding="true"
     android:orientation="vertical"
     android:paddingStart="12dp"
     android:singleLine="true"
     android:textAlignment="viewStart"
     android:textColor="#000"
     android:textDirection="locale"
     android:textSize="18dp"
     tools:text="alskjfdn" />


键盘隐藏时如何清除焦点?键盘隐藏后光标仍显示。

使用如下方式:

public void hideKeyboard(Activity activity){
  InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  View view = activity.getCurrentFocus();
  if(view != null){
    view.clearFocus();
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
  }
}

使用类似以下内容:

public void hideKeyboard(Activity activity){
  InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  View view = activity.getCurrentFocus();
  if(view != null){
    view.clearFocus();
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
  }
}

在键盘内部关闭侦听器,关闭键盘后清除editText中的焦点

editText.setFocusable(false);
editText.setFocusableInTouchMode(true);

在键盘内部关闭侦听器,关闭键盘后清除editText中的焦点

editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
可能的重复可能的重复