[英]How to clear focus from edittext when the keyboard closes after click back button?
我有一個AppBarLayout
,當我觸摸EditText
時隱藏了。
SearchFragment.class
binding.searchEt.setOnFocusChangeListener { view, hasFocus ->
if (hasFocus) {
binding.appBarLayout.setExpanded(false, true)
} else {
binding.appBarLayout.setExpanded(true, true)
}
}
但是當鍵盤關閉時,我無法再顯示AppBarLayout
。 因為當鍵盤關閉時EditText
焦點不會改變。
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);
}
}
在鍵盤內部關閉監聽器后,在鍵盤關閉后從editText清除焦點
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.