Android 创建自定义 SearchView 搜索操作
在我的应用程序中,我使用带有自定义 SearchView 的操作栏。当用户点击搜索字段中的输入按钮时,我想使用一些自定义代码。但我找不到一种方法来捕捉用户在 SearchView 中点击输入按钮的那一刻。
现在我尝试了几种方法来捕获搜索操作,但我似乎找不到它。
我的按钮定义如下:
<item
android:id="@+id/action_bar_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="-package-.CustomSearchView"
/>
这属于 CustomSearchView 类:
public class CustomSearchView extends SearchView implements OnClickListener, android.view.View.OnKeyListener{
public CustomSearchView(Context context) {
super(context);
this.setOnSearchClickListener(this);
this.setSubmitButtonEnabled(true);
this.setOnClickListener(this);
this.setOnKeyListener(this);
}
@Override
public void onClick(View v) {
Log.d("SEARCH", "Onclick! " + this.getQuery());
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d("SEARCH", "Search onkey");
return false;
}
}
在这里您可以看到我尝试了 2 种方法:
- 从提交按钮捕获 onclick 事件。那不起作用 因为它仅在我单击搜索图标打开时触发 搜索视图。
- 在 SearchView 中捕捉击键。那不起作用 任何一个。我在那里按的任何键都没有响应。
有谁有办法使用此 SearchView 进行自定义搜索操作?
更新:
正如 PJL 所说:您不需要自定义对话框来覆盖此行为。普通的 SearchView 小部件就可以很好地实现此目的。
In my application I am using the Action Bar with a custom SearchView in it. I want to use some custom code when the user hits the enter button in the search field. But I cant find a way to catch the moment the user hits the enter button in the SearchView.
Now I tried several ways to capture the search action but I cant seem to find it.
My button is defined like this:
<item
android:id="@+id/action_bar_search"
android:title="Search"
android:icon="@drawable/ic_menu_search"
android:showAsAction="always"
android:actionViewClass="-package-.CustomSearchView"
/>
With this belongs the CustomSearchView class:
public class CustomSearchView extends SearchView implements OnClickListener, android.view.View.OnKeyListener{
public CustomSearchView(Context context) {
super(context);
this.setOnSearchClickListener(this);
this.setSubmitButtonEnabled(true);
this.setOnClickListener(this);
this.setOnKeyListener(this);
}
@Override
public void onClick(View v) {
Log.d("SEARCH", "Onclick! " + this.getQuery());
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
Log.d("SEARCH", "Search onkey");
return false;
}
}
Here you can see that I`ve tried 2 approaches:
- Catch the onclick event from the submit button. That didnt work
since it only fired when I clicked on the search icon to open the
SearchView. - Catch keystrokes within the SearchView. That didnt work
either. Got no response from any key that I pressed there.
Does anyone have a way of making a custom Search action with this SearchView?
UPDATE:
As stated below by PJL: You dont need a custom dialog for overriding this behaviour. The normal SearchView widget will work just fine for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
获取您的搜索视图并添加一个
OnQueryTextListener
顺便说一句,我的菜单布局如下;
Get your search view and add an
OnQueryTextListener
By the way my menu layout is as follows;