我试图用一个搜索查看,我得到的一切,除非我想寻找一个空字符串工作。 该onQueryTextChange没有反应过来的时候我删除的最后一个字符,但我希望用户能够preSS搜索按钮时searchfield是空的。
I'm trying to use a SearchView and I got everything to work, except when I want to search an empty string. The onQueryTextChange does react when I remove the last character, but I want the user to be able to press the search button when the searchfield is empty.
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);
我也试着使用OnKeyListner。但它似乎并没有擦出火花。
I've also tried using a OnKeyListner. but it does not seem to work either.
searchView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
//Do something
return true;
}
});
这似乎是这样一个简单的事情,但我不能得到它的工作。有什么建议?
This seems such a simple thing to do, but I can't get it to work. Any suggestions?
修改
我现在已经找了一段时间的解决方案,只是数分钟发布此之后,我找到了解决办法。 在这个线程我发现这不是一个错误,但它实际上是经过深思熟虑的。
I have looked for a solution for a while now and just some minutes after posting this, I found a solution. On this thread I found out this was not a bug, but it actually was deliberate.
Android SearchView.OnQueryTextListener OnQueryTextSubmit没有开枪空的查询字符串
所以,我刚刚下载 ActionBarSherlock ,并提出了一些改进的方法onSubmitQuery()
So I just downloaded ActionBarSherlock and made some modification to the method onSubmitQuery()
在
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if (query != null && TextUtils.getTrimmedLength(query) > 0) {
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
}
和修改后的版本
private void onSubmitQuery() {
CharSequence query = mQueryTextView.getText();
if(query == null) {query = "";}
if (mOnQueryChangeListener == null
|| !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
if (mSearchable != null) {
launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
setImeVisibility(false);
}
dismissSuggestions();
}
}
希望这有助于其他人是否有这个问题。
Hope this helps if anyone else is having this problem.
推荐答案
我有同样的问题,搜索查看,但我不希望使用ActionBarSherlock,所以我想出了解决方案,其中包括造型你自己的搜索查看,如图中的指导 http://novoda.com/blog/styling-the-actionbar-searchview/ 并设置OnEditorActionListener为的EditText(How做我触发当用户按下回车键?),它是搜索查看的一部分行动。
I had same issue with SearchView but I did not want to use ActionBarSherlock so I came up with solution which consists of styling your own SearchView as shown in guide http://novoda.com/blog/styling-the-actionbar-searchview/ and setting OnEditorActionListener for EditText( How do I trigger an action when the user has hit enter?) which is part of the SearchView.
final SearchView searchView = (SearchView)findViewById(R.id.search);
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//Do something
}
return false;
});
相关推荐
最新文章