Android 如何在searchView中收听键盘搜索按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26306717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to listen to keyboard search button in searchView
提问by learner
I have a SearchView. When the user clicks on the keyboardsearch button, I need to make a server call. What does the code for the listener look like? I am thinking I have to use OnClickListener. But the internal code for knowing it's the search button, I am not sure how to determine that.
我有一个搜索视图。当用户单击键盘搜索按钮时,我需要进行服务器调用。侦听器的代码是什么样的?我想我必须使用 OnClickListener。但是知道它是搜索按钮的内部代码,我不确定如何确定。
回答by Qadir Hussain
I have done like this
我已经这样做了
the onQueryTextSubmit
is the method you are looking for.
这onQueryTextSubmit
是您正在寻找的方法。
set setOnQueryTextListener
on your search view.
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;
}
hope this help
希望这有帮助
回答by arjunaskykok
This is the kotlin version:
这是科特林版本:
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
}
})
回答by Karanvir Singh
Here you can go for the Android best answer not Kotlin
在这里,您可以选择 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;
}
});
Make sure you have added following line in your EditText
确保您在 EditText 中添加了以下行
android:imeOptions="actionSearch"
Just like following:-
就像下面这样:-
<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"/>
I hope this will help you to solve your issue.!!!!
我希望这能帮助你解决你的问题。!!!!!!
回答by DjP
SearchView method
搜索视图方法
setOnQueryTextLister
setOnQueryTextLister
will do your work as below shown.
将完成您的工作,如下所示。
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
showSnackBar(query.toString());
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
Here searchView is object of SearchView.
这里 searchView 是 SearchView 的对象。