问题描述
我注意到,有没有可行的解决方案,显示了如何使用过滤器使用的动作条一个ListView项目,在旧的Android版本的作品(如2.3.x)
我发现的唯一的例子是在片段的例子文件LoaderCursorSupport.java。但是,只有当搜索查看可以创建,这意味着从安卓3.X开始,如在code工作:
查看搜索查看= SearchViewCompat.newSearchView(getActivity());
如果(搜索查看!= NULL)
...
上面的错误(或缺少的功能,无论你如何看待它的方式)仍然存在,甚至在版本4.2 actionBarSherlock的。
所以,我做了我自己的解决方案,它的伟大工程(祝官库可以加我修复它太),但我不知道在哪里的EDITTEXT视图中获得的X按钮负责清除文本。
谁能告诉我如何得到本机的外观和感觉,并把它正确地在code?
下面是我所谈论的是截图:
对于那些希望使用这一功能,这是我的code片断:
@覆盖
公共布尔onCreateOptionsMenu(最终com.actionbarsherlock.view.Menu菜单)
{
。getSupportMenuInflater()膨胀(R.menu.activity_main,菜单);
_searchMenuItem = menu.findItem(R.id.menu_item_action_search);
查看搜索查看= SearchViewCompat.newSearchView(本);
如果(搜索查看!= NULL)
SearchViewCompat.setOnQueryTextListener(搜索查看,新OnQueryTextListenerCompat()
{
@覆盖
公共布尔onQueryTextChange(最后弦乐newText)
{
。_listAdapter.getFilter()过滤器(newText);
返回true;
}
@覆盖
公共布尔onQueryTextSubmit(最后的查询字符串)
{
返回super.onQueryTextSubmit(查询);
}
});
其他
{
搜索查看=新的EditText(本);
searchView.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
((EditText上)搜索查看).setHint(R.string.search);
((EditText上)搜索查看).addTextChangedListener(新TextWatcher()
{
字符串curretTextToFilter = NULL;
@覆盖
公共无效onTextChanged(最终的CharSequence newText,最终诠释开始,最终诠释之前,最后的诠释计数)
{
如果(newText == curretTextToFilter)
返回;
curretTextToFilter = newText.toString();
。_listAdapter.getFilter()过滤器(curretTextToFilter == NULL || curretTextToFilter.length()== 0空:curretTextToFilter);
}
@覆盖
公共无效beforeTextChanged(最终CharSequence中,最终诠释开始,最终的诠释计数,最终诠释后)
{}
@覆盖
公共无效afterTextChanged(最终可编辑S)
{}
});
}
最后查看finalSearchView =搜索查看;
_searchMenuItem.setOnActionExpandListener(新OnActionExpandListener()
{
@覆盖
公共布尔onMenuItemActionExpand(最后一个菜单项项)
{
如果(finalSearchView的instanceof的EditText)
{
最后InputMethodManager M =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
finalSearchView.requestFocus();
如果(M!= NULL)
m.toggleSoftInput(0,InputMethodManager.SHOW_IMPLICIT);
}
返回true;
}
@覆盖
公共布尔onMenuItemActionCollapse(最后一个菜单项项)
{
如果(finalSearchView的instanceof的EditText)
((EditText上)finalSearchView).setText(空);
。其他_listAdapter.getFilter()过滤器(空);
返回true;
}
});
_searchMenuItem.setActionView(搜索查看);
//
返回true;
}
@覆盖
公共布尔的onkeyup(最终诠释键code,最终KeyEvent的事件)
{
如果(键code == KeyEvent.KEY code_SEARCH)
{
_searchMenuItem.expandActionView();
返回true;
}
返回super.onKeyUp(键code,事件);
}
除了X按钮,我想剩下的可以通过使用一个库,一个主题,叫的。
I've noticed that there is no working solution that shows how to use filter on a listView items using the actionbar, that works on older Android versions (like 2.3.x).
The only example I've found is in the file "LoaderCursorSupport.java" of the fragments example. However, it only works when the searchView can be created, meaning starting from Android 3.x, as shown in the code:
View searchView=SearchViewCompat.newSearchView(getActivity());
if(searchView!=null)
...
The above bug (or missing feature, whichever way you look at it) still exist even on version 4.2 of actionBarSherlock.
So I've made my own solution, which works great (and I wish the official library could add my fix to it too), but I don't know where to get the "x" button within the editText view that is responsible for clearing the text.
Can anyone please tell me how to get the native look and feel and put it correctly in the code?
Here's a screenshot of what i'm talking about:
For those wish to use this feature, here is my code snippet :
@Override
public boolean onCreateOptionsMenu(final com.actionbarsherlock.view.Menu menu)
{
getSupportMenuInflater().inflate(R.menu.activity_main,menu);
_searchMenuItem=menu.findItem(R.id.menu_item_action_search);
View searchView=SearchViewCompat.newSearchView(this);
if(searchView!=null)
SearchViewCompat.setOnQueryTextListener(searchView,new OnQueryTextListenerCompat()
{
@Override
public boolean onQueryTextChange(final String newText)
{
_listAdapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(final String query)
{
return super.onQueryTextSubmit(query);
}
});
else
{
searchView=new EditText(this);
searchView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
((EditText)searchView).setHint(R.string.search);
((EditText)searchView).addTextChangedListener(new TextWatcher()
{
String curretTextToFilter =null;
@Override
public void onTextChanged(final CharSequence newText,final int start,final int before,final int count)
{
if(newText==curretTextToFilter)
return;
curretTextToFilter=newText.toString();
_listAdapter.getFilter().filter(curretTextToFilter==null||curretTextToFilter.length()==0 ? null : curretTextToFilter);
}
@Override
public void beforeTextChanged(final CharSequence s,final int start,final int count,final int after)
{}
@Override
public void afterTextChanged(final Editable s)
{}
});
}
final View finalSearchView=searchView;
_searchMenuItem.setOnActionExpandListener(new OnActionExpandListener()
{
@Override
public boolean onMenuItemActionExpand(final MenuItem item)
{
if(finalSearchView instanceof EditText)
{
final InputMethodManager m=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
finalSearchView.requestFocus();
if(m!=null)
m.toggleSoftInput(0,InputMethodManager.SHOW_IMPLICIT);
}
return true;
}
@Override
public boolean onMenuItemActionCollapse(final MenuItem item)
{
if(finalSearchView instanceof EditText)
((EditText)finalSearchView).setText(null);
else _listAdapter.getFilter().filter(null);
return true;
}
});
_searchMenuItem.setActionView(searchView);
//
return true;
}
@Override
public boolean onKeyUp(final int keyCode,final KeyEvent event)
{
if(keyCode==KeyEvent.KEYCODE_SEARCH)
{
_searchMenuItem.expandActionView();
return true;
}
return super.onKeyUp(keyCode,event);
}
Except for the "X" button, I think the rest can be done using a library with a theme, called HoloEverywhere.
这篇关于使用的actionBarSherlock老版本的Android原生外观搜索查看和感觉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!