Warm tip: This article is reproduced from stackoverflow.com, please click
android xml

Trying to make the soft keyboard enter button say search or display search magnifying glass and hand

发布于 2020-03-30 21:16:38

I have tried the highly popular answer suggested here

Although this hasn't worked for me like it seems to have for the other users. Perhaps it is outdated or I have something conflicting in my code.

Here is the xml for my EditText:

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

As you can see I have the android:imeOptions="search" & android:inputType="text" along with textNoSuggestions & textVisiblePassword as the inputType. I have tried this with only text as the inputType like the other answer suggested but this didn't make any difference and I don't want any suggestions on the keyboard.

As for this listener I managed to get that working although in a bit of a hacky way because the action id is coming back as IME_ACTION_UNSPECIFIED, So I just handled this instead. This can be seen here:

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

I've currently got an icon for a new line rather than search or a search icon.

Why doesn't the xml seem to be affected by android:imeOptions="search" in my case?

Why is the enter button displaying a new line icon?

Why is the action id returning as unspecified?

Thanks

Questioner
COYG
Viewed
68
Paul Ost 2020-01-31 19:07

Remove

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"

from your EditText and it will fix your problem.

The conflict was due to the android:digits is set. In this situation android:inputType is set to numeric by default disregarding what you entered there(android:inputType="text|textNoSuggestions|textVisiblePassword") in your xml, thus making your android:imeOptions="actionSearch" irrelevant.

Hope it helps.