Подтвердить что ты не робот

Как слушать кнопку поиска клавиатуры в searchView

У меня есть SearchView. Когда пользователь нажимает кнопку поиска клавиатуры, мне нужно сделать вызов сервера. Как выглядит код для слушателя? Я думаю, что мне нужно использовать OnClickListener. Но внутренний код, зная его кнопку поиска, я не уверен, как это определить.

4b9b3361

Ответ 1

Я сделал вот это

onQueryTextSubmit - это метод, который вы ищете.

установите setOnQueryTextListener в окне поиска.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem searchItem = menu.findItem(R.id.search_city);
    searchView = (SearchView) searchItem.getActionView();
    searchView.setQueryHint("Search View Hint");

    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextChange(String newText) {
            //Log.e("onQueryTextChange", "called");
            return false;
        }

        @Override
        public boolean onQueryTextSubmit(String query) {


            // Do your task here

            return false;
        }

    });

    return true;
}

надеюсь, что эта помощь

Ответ 2

Метод SearchView

setOnQueryTextLister

будет делать вашу работу, как показано ниже.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                showSnackBar(query.toString());
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

Здесь searchView является объектом SearchView.

Ответ 3

Это версия Kotlin:

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

            override fun onQueryTextChange(s: String): Boolean {
                // make a server call
                return true
            }

            override fun onQueryTextSubmit(s: String): Boolean {
                val intent = Intent(applicationContext, YourSearchableActivity::class.java)
                intent.putExtra(SearchManager.QUERY, s)
                intent.putExtra(CUSTOM_MESSAGE, "you can also add custom message when submitting the search action")
                intent.setAction(Intent.ACTION_SEARCH)
                startActivity(intent)
                return true
            }
        })

Ответ 4

Здесь вы можете пойти на Android лучший ответ не Kotlin

        binding.edtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // Your piece of code on keyboard search click
                if (AppValidator.isValid(SearchEventsActivity.this, binding.edtSearch, "Please enter your search keyword.")) {
                    searchKeyword = binding.edtSearch.getText().toString().trim();
                    //Search Events categories
                    callSearchEventsApi();
                }

                return true;
            }
            return false;
        }
    });

Убедитесь, что вы добавили следующую строку в ваш EditText

android:imeOptions="actionSearch"

Так же, как следующие: -

 <EditText
                android:id="@+id/edtSearch"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="@dimen/_15dp"
                android:background="@null"
                android:fontFamily="@font/roboto_regular"
                android:textColorHint="@color/text_color_light"
                android:layout_gravity="center"
                android:textSize="@dimen/_16sp"
                android:maxLines="1"
                android:singleLine="true"
                android:imeOptions="actionSearch"
                android:hint="@string/search"/>

Я надеюсь, что это поможет вам решить вашу проблему. !!!!