如何监听SearchView中键盘搜索按钮的按下事件

20

我有一个SearchView。当用户点击键盘搜索按钮时,我需要进行服务器调用。监听器的代码是什么样的?我想我必须使用OnClickListener。但是确定它是搜索按钮的内部代码,我不确定如何确定。


它发送KeyEvent#KEYCODE_SEARCH键,因此您可以使用onKeyEventListener或activity的dispatchKeyEvent来查找它。 - DeeV
@DeeV 文档中说onKeyEventListener是用于硬件按键的。如果它同样适用于软键盘,为什么要特别指出硬件呢?无论如何,既然你说这是答案,我会尝试一下的。 - learner
@DeeV 在提问之前,我已经尝试了onEditorActionListener。但是SearchView没有识别它。 - learner
展示一下你的搜索视图代码。 - Qadir Hussain
1
@QadirHussain,你真的理解这个问题吗?你的万能评论在这里不适用。 - Konsol Labapen
这里有个有趣的解决方法:SearchView监听IME操作 - Richard Le Mesurier
4个回答

73

我已经这么做了。

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

希望这能帮到你


1
耶!好的,你比我更快地回應了。我會點贊! - Konsol Labapen

2

这是 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
            }
        })

2

这里是关于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中添加了以下行:
确保您在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"/>

我希望这能帮助你解决问题。最初的回答:

我希望这个能够对你有所帮助,解决你的问题!


0

SearchView方法

setOnQueryTextListener

将会像下面展示的那样完成你的工作。

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 对象。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接