简体   繁体   English

当单击后退按钮后键盘关闭时,如何从edittext清除焦点?

[英]How to clear focus from edittext when the keyboard closes after click back button?

I have an AppBarLayout and I hide when I touch EditText . 我有一个AppBarLayout ,当我触摸EditText时隐藏了。

SearchFragment.class SearchFragment.class

 binding.searchEt.setOnFocusChangeListener { view, hasFocus ->
        if (hasFocus) {
            binding.appBarLayout.setExpanded(false, true)
        } else {
            binding.appBarLayout.setExpanded(true, true)
        }
    }

But I can't show again AppBarLayout when keyboard closes. 但是当键盘关闭时,我无法再显示AppBarLayout Because when keyboard closes EditText focus does not change. 因为当键盘关闭时EditText焦点不会改变。

Xml 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" />

How can I clear focus when keyboard hide? 隐藏键盘时如何清除焦点? Cursor still showing after keyboard hidden. 隐藏键盘后,光标仍显示。

use something like this: 使用这样的东西:

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);
  }
}

inside the keyboard close listener, after close keyboard clear focus from editText 在键盘内部关闭监听器后,在键盘关闭后从editText清除焦点

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下按钮时清除编辑文本焦点并隐藏键盘 - Clear edittext focus and hide keyboard when button is pressed 当显示软键盘时按下后退按钮时清除焦点EditText - Clear focus EditText when back button is pressed when softkeyboard is showing 单击它时,重点放在 EditText 上很清楚 - Focus on EditText is clear when click on it Android:EditText 打开键盘时处理后退按钮单击 - Android : Handle back button click when keyboard is open for EditText 如何停止编辑文本以将焦点更改为单击片段中的后退按钮? - How to stop the edittext to change the focus on the click of the back button in fragment? 按下软键盘中的“完成”按钮时如何失去编辑文本的焦点? - How to lose the focus of a edittext when "done" button in the soft keyboard is pressed? Android:键盘关闭后,带有EditText的DrawerLayout按下时会关闭应用程序 - Android: DrawerLayout with EditText after keyboard dismissed closes app when pressing back 当另一个EditText清晰焦点时,停止EditText获得焦点 - Stop EditText from gainning focus when another EditText clear focus 单击或触摸其他视图时,清除EditText焦点并隐藏键盘 - Clear EditText focus and hide keyboard when Clicked or Touched another Views Android:单击按钮时如何打开键盘以编辑EditText? - Android: How to open keyboard for editing EditText when click button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM