簡體   English   中英

當單擊后退按鈕后鍵盤關閉時,如何從edittext清除焦點?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM