Użyj klawisza "ENTER" na softkeyboard zamiast klikania przycisku

Witam Mam wyszukiwane EditText i Szukaj Button. Kiedy wpiszę wyszukiwany tekst, chciałbym użyć klawisza ENTER na softkeyboard zamiast search Button, Aby aktywować funkcję wyszukiwania.

Z góry dzięki za pomoc.
Author: Peter O., 2010-12-15

6 answers

Robisz to ustawiając OnKeyListener na swoim EditText.

Oto przykład z mojego własnego kodu. Mam EditText o nazwie addCourseText, która wywoła funkcję addCourseFromTextBox Po kliknięciu klawisza enter lub padu kierunkowego.
addCourseText = (EditText) findViewById(R.id.clEtAddCourse);
addCourseText.setOnKeyListener(new OnKeyListener()
{
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if (event.getAction() == KeyEvent.ACTION_DOWN)
        {
            switch (keyCode)
            {
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
                    addCourseFromTextBox();
                    return true;
                default:
                    break;
            }
        }
        return false;
    }
});
 127
Author: Nailuj,
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
2010-12-15 15:43:54
<EditText
    android:id="@+id/search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search_hint"
    android:inputType="text"
    android:imeOptions="actionSend" />

Możesz następnie nasłuchać naciśnięć przycisku akcji, definiując widok tekstu.OnEditorActionListener dla elementu EditText. W swoim słuchaczu odpowiadaj na odpowiedni identyfikator akcji IME zdefiniowany w klasie EditorInfo, taki jak IME_ACTION_SEND. Na przykład:

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

Źródło: https://developer.android.com/training/keyboard-input/style.html

 31
Author: tieorange,
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-07-05 20:14:59

Być może możesz dodać atrybut do swojego EditText w następujący sposób:

android:imeOptions="actionSearch"
 25
Author: itemon,
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
2010-12-15 16:07:24

Dodaj atrybut do EditText jak android: imeOptions= "actionSearch"

To jest najlepszy sposób na wykonanie funkcji

I imeOptions mają również inne wartości, takie jak "go", "next"," done " itp.

 5
Author: ruyi.zhu,
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
2011-01-24 12:20:24

Aby uniknąć przesunięcia ostrości do następnego pola edytowalnego (jeśli je posiadasz), możesz zignorować Zdarzenia typu key-down, ale obsłużyć Zdarzenia typu key-up. Wolę też najpierw filtrować kod klucza, zakładając, że będzie on nieco bardziej wydajny. Przy okazji, pamiętaj, że zwracanie true oznacza, że zająłeś się wydarzeniem, więc żaden inny słuchacz tego nie zrobi. W każdym razie, oto moja wersja.

ETFind.setOnKeyListener(new OnKeyListener()
{
    public boolean onKey(View v, int keyCode, KeyEvent event)
    {
        if (keyCode ==  KeyEvent.KEYCODE_DPAD_CENTER
        || keyCode ==  KeyEvent.KEYCODE_ENTER) {

            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                // do nothing yet
            } else if (event.getAction() == KeyEvent.ACTION_UP) {
                        findForward();      
            } // is there any other option here?...

            // Regardless of what we did above,
            // we do not want to propagate the Enter key up
            // since it was our task to handle it.
            return true;

        } else {
            // it is not an Enter key - let others handle the event
            return false;
        }
    }

});
 0
Author: Wojtek Jarosz,
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
2013-04-26 09:24:45

To jest próbka jednej z moich aplikacji, jak sobie radzę

 //searching for the Edit Text in the view    
    final EditText myEditText =(EditText)view.findViewById(R.id.myEditText);
        myEditText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                 if (event.getAction() == KeyEvent.ACTION_DOWN)
                      if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
                             (keyCode == KeyEvent.KEYCODE_ENTER)) {
                                //do something
                                //true because you handle the event
                                return true;
                               }
                       return false;
                       }
        });
 0
Author: Manuel Alejandro Diaz Serret,
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
2015-10-12 14:43:41