在我的应用程序,我现在用的操作栏,在它的自定义搜索查看。我想用一些自定义的code,当用户点击进入按钮,在搜索领域。但我不能找到一种方法来捕捉用户点击了搜索查看输入按钮的那一刻。
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"
/>
通过这种所属的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;
}
}
在这里,你可以看到,I`ve试过2的方法:
Here you can see that I`ve tried 2 approaches:
赶上从提交按钮的onclick事件。没有工作 因为它只能发射当我点击搜索图标,打开上 搜索查看。 的搜索查看内捕获键击。没有工作 无论是。有没有从我pressed有任何按键响应。 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?
更新:
正如下面指出的PJL:你不需要一个自定义对话框覆盖这一行为。正常的搜索查看部件将工作得很好这一点。
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.
让你的搜索视图并添加 OnQueryTextListener
Get your search view and add an OnQueryTextListener
final SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
// Do something
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
顺便说一句我的菜单布局如下:
By the way my menu layout is as follows;
<item android:id="@+id/menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView" />