![](/img/trans.png)
[英]Android: how to make keyboard enter button say "Search" and handle its click?
[英]Trying to make the soft keyboard enter button say search or display search magnifying glass and handle its click for an EditText
我已經嘗試了這里建議的非常受歡迎的答案
盡管這對我來說並沒有像其他用戶那樣對我有用。 也許它已經過時了,或者我的代碼中有一些沖突。
這是我的EditText
的 xml:
<EditText
android:id="@+id/editText_letter_entry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/edit_text_rounded"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
android:drawableEnd="@drawable/ic_search_black_24dp"
android:hint="@string/enter_letters"
android:importantForAutofill="no"
android:inputType="text|textNoSuggestions|textVisiblePassword"
android:maxLength="15"
android:maxLines="1"
android:imeOptions="actionSearch"
android:paddingLeft="@dimen/et_padding_left_right"
android:paddingRight="@dimen/et_padding_left_right"
app:layout_constraintBottom_toTopOf="@+id/bottomBannerAdView"
app:layout_constraintEnd_toStartOf="@+id/guideline2Container"
app:layout_constraintStart_toStartOf="@+id/guideline1Container"
app:layout_constraintTop_toTopOf="parent" />
如您所見,我將android:imeOptions="search" & android:inputType="text"
以及textNoSuggestions
& textVisiblePassword
作為 inputType。 我已經像其他答案建議的那樣僅使用text
作為inputType
進行了嘗試,但這沒有任何區別,我不希望在鍵盤上有任何建議。
至於這個偵聽器,我設法IME_ACTION_UNSPECIFIED
工作,盡管以一種有點hacky的方式工作,因為action id
作為IME_ACTION_UNSPECIFIED
,所以我只是處理了這個。 這可以在這里看到:
et_letter_entry.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED ) {
preformSearch();
return true;
}
return false;
}
});
我目前有一個新行的圖標,而不是search
或搜索圖標。
在我的情況下,為什么 xml 似乎不受android:imeOptions="search"
?
為什么回車按鈕顯示一個新行圖標?
為什么操作 ID 返回未指定?
謝謝
消除
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
來自您的EditText
,它將解決您的問題。
沖突是由於設置了android:digits
。 在這種情況下,默認情況下android:inputType
設置為numeric
,而不管您在 xml 中輸入的內容( android:inputType="text|textNoSuggestions|textVisiblePassword"
),從而使您的android:imeOptions="actionSearch"
無關緊要。
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.