Android: jak sprawić, aby klawiatura klawisz enter powiedział "Szukaj" i obsługiwał jego kliknięcie?

Nie mogę tego rozgryźć. Niektóre aplikacje mają EditText (textbox), który po dotknięciu i wyświetla klawiaturę ekranową, klawiatura ma przycisk "Szukaj" zamiast klawisza enter.

Chcę to zaimplementować. Jak zaimplementować ten przycisk wyszukiwania i wykryć naciśnięcie przycisku wyszukiwania?

Edit : znaleziono jak zaimplementować przycisk wyszukiwania; w XML, android:imeOptions="actionSearch" lub w Javie, EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);. Ale jak poradzić sobie z użytkownikiem naciskającym ten przycisk wyszukiwania? Czy ma coś do zrobić z android:imeActionId?

Author: MiguelHincapieC, 2010-07-08

4 answers

W układzie ustaw opcje metody wprowadzania do wyszukiwania.

<EditText
    android:imeOptions="actionSearch" 
    android:inputType="text" />

W Javie Dodaj akcję Editor listener.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }
        return false;
    }
});
 794
Author: Robby Pond,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-23 17:59:33

Ukryj klawiaturę, gdy użytkownik kliknie Szukaj. Dodatek do Robby Pond odpowiedz

private void performSearch() {
    editText.clearFocus();
    InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
    ...perform search
}
 10
Author: kaMChy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-16 08:50:23

W pliku xml, umieścić imeOptions="actionSearch" i inputType="text", maxLines="1":

<EditText
    android:id="@+id/search_box"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search"
    android:imeOptions="actionSearch"
    android:inputType="text"
    android:maxLines="1" />
 4
Author: Braj Bhushan Singh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-19 10:33:59

W Kotlinie

evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        doTheLoginWork()
    }
    true
}

Częściowy Kod Xml

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
       <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp">

            <EditText
                android:id="@+id/evLoginUserEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/email"
                android:inputType="textEmailAddress"
                android:textColor="@color/black_54_percent" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp">

            <EditText
                android:id="@+id/evLoginPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/password"
                android:inputType="textPassword"
                android:imeOptions="actionDone"
                android:textColor="@color/black_54_percent" />
        </android.support.design.widget.TextInputLayout>
</LinearLayout>
 0
Author: Shaon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-28 06:02:03