Android 如何在 Action Bar Sherlock 中实现搜索小部件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11690525/
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 implement search widget in Action Bar Sherlock?
提问by Hick
I tried to get Search box to work on Action Bar Sherlock.
我试图让搜索框在 Action Bar Sherlock 上工作。
This is my PreLocationActivity
:
这是我的PreLocationActivity
:
@ContentView(R.layout.search)
public class PreLocationActivity extends RoboSherlockActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.map_layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
menu.add("Search")
.setIcon(R.drawable.ic_search_inverse)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
@Override
public boolean onSearchRequested() {
return super.onSearchRequested();
}
}
This is my SearchableActivity
:
这是我的SearchableActivity
:
@ContentView(R.layout.search)
public class SearchableActivity extends RoboSherlockFragmentActivity {
@InjectView(R.id.addressListView) ListView addressView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doGeoSearch(query);
}
}
public void doGeoSearch(String query){
Geocoder geocoder;
ArrayList<Address> addresses;
ArrayList<String> address = new ArrayList<String>() ;
geocoder = new Geocoder(this, Locale.getDefault());
try {
addresses = (ArrayList<Address>) geocoder.getFromLocationName(query, 6);
Log.d("Address",String.valueOf(addresses));
for(int i = 0;i<addresses.size();i++)
{
String addr = new String();
addr.concat(addresses.get(i).getAddressLine(0));
addr.concat(addresses.get(i).getAddressLine(1));
addr = addresses.get(i).getAddressLine(0) + addresses.get(i).getLocality() + addresses.get(i).getAdminArea();
//addr.concat(addresses.get(i).getAddressLine(2));
Log.d("addr",addr);
address.add(addr);
}
SearchAddressAdapater addressList = new SearchAddressAdapater(getApplicationContext(),R.layout.search_list,addresses, SearchableActivity.this);
addressView.setAdapter(addressList);
//ListView addressListView = new ListView();
} catch (IOException e) {
//Handle exception
}
}
No success at all. As in, when I type something on the Prelocation Activity and press enter, nothing is being searched. Do I have to treat it as an EditText
and write a text listener for that which then calls the geoCoder
and gets me the locations or is there a smarter way to go about it?
根本没有成功。比如,当我在 Prelocation Activity 上输入内容并按 Enter 键时,没有搜索到任何内容。我是否必须将其视为EditText
一个文本侦听器并为其编写一个文本侦听器,然后调用该侦听器geoCoder
并获取位置,或者是否有更聪明的方法来解决它?
采纳答案by Georgy Gobozov
private EditText search;
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu)
{
menu.add(0, 1, 1, R.string.menu_search).setIcon(R.drawable.ic_action_search).setActionView(R.layout.action_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
{
switch (item.getItemId())
{
case 1:
search = (EditText) item.getActionView();
search.addTextChangedListener(filterTextWatcher);
search.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
private TextWatcher filterTextWatcher = new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// your search logic here
}
};
回答by Jared Burrows
Android Support Library(v4 + v7):
Android 支持库(v4 + v7):
Support Library v7:http://developer.android.com/tools/support-library/features.html#v7-appcompat
支持库 v7:http : //developer.android.com/tools/support-library/features.html#v7-appcompat
Google now supports the ActionBar compatibility back to Android 2.1(API 7).
Google 现在支持 ActionBar 兼容性回到 Android 2.1(API 7)。
It is easy to make the transition because the method names are the same and/or very similar.
由于方法名称相同和/或非常相似,因此很容易进行转换。
Add the Support Library with Resources:http://developer.android.com/tools/support-library/setup.html#libs-with-res
添加带有资源的支持库:http : //developer.android.com/tools/support-library/setup.html#libs-with-res
Your Manifest: AndroidManifest.xml
你的清单:AndroidManifest.xml
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
Your Menu: menu.xml
您的菜单:menu.xml
<item
android:id="@+id/menu_search"
android:actionViewClass="android.support.v7.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="Search"/>
ActionBarSherlock [deprecated, use appcompat]:
ActionBarSherlock [已弃用,使用 appcompat]:
Here is how to use the standard SearchView and SearchManager in Android with ActionBarSherlock! I am using this code and works fine. I have tested this on Android 2.3(API 10) - Android 4.3(API 18).
下面是如何在 Android 中通过 ActionBarSherlock 使用标准的 SearchView 和 SearchManager!我正在使用此代码并且工作正常。我已经在 Android 2.3(API 10) - Android 4.3(API 18) 上测试过了。
Great Tutorial and Documentation:
很棒的教程和文档:
Keep in mind:
记住:
Custom Search with ActionBarSherlock(min. API 7)
使用 ActionBarSherlock 自定义搜索(最低 API 7)
SearchView with ActionBarSherlock(min. API 8)
带有 ActionBarSherlock 的 SearchView(最低 API 8)
Your Menu: menu.xml
您的菜单:menu.xml
<item
android:id="@+id/menu_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"
android:icon="@drawable/ic_action_search"
android:showAsAction="ifRoom|collapseActionView"
android:title="Search"/>
For Both:
对彼此而言:
Your Activity: MainActivity.java
您的活动:MainActivity.java
public boolean onCreateOptionsMenu(Menu menu)
{
getSupportMenuInflater().inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
if (null != searchView )
{
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
}
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener()
{
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(newText);
return true;
}
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
return true;
}
};
searchView.setOnQueryTextListener(queryTextListener);
return super.onCreateOptionsMenu(menu);
}
Let me know if this works for you as well and let me know if you need anymore help!
让我知道这是否也适用于您,如果您需要更多帮助,请告诉我!
回答by Javier Salinas
A similar question there are here where I had the problem to assign the adapter.
这里有一个类似的问题,我在分配适配器时遇到了问题。
How to implement search widget with a listview using SherlockActionbar?
如何使用 SherlockActionbar 实现带有列表视图的搜索小部件?
I hope that it would help because I had the same problem.
我希望它会有所帮助,因为我遇到了同样的问题。
To use the adapter the best way is that the class implements SearchView.OnQueryTextListener and then you don′t have to create the inner class and you will have the adapter.
使用适配器最好的方法是该类实现 SearchView.OnQueryTextListener ,然后您不必创建内部类,您将拥有适配器。
In your code will be something as:
在您的代码中将是这样的:
public class MainActivity extends SherlockListActivity implements SearchView.OnQueryTextListener
and then in the class you have to define the methods. The adapter will be the adapter that you have in your class ArrayAdapter adapter. But you should define it as private in the class.
然后在类中你必须定义方法。适配器将是您在类 ArrayAdapter 适配器中的适配器。但是您应该在类中将其定义为私有。
public boolean onQueryTextChange(String newText) {
// this is your adapter that will be filtered
adapter.getFilter().filter(newText);
return true;
}
public boolean onQueryTextSubmit(String query) {
// this is your adapter that will be filtered
adapter.getFilter().filter(query);
return true;
}
In the line where you are setting:
在您设置的行中:
searchView.setOnQueryTextListener(queryTextListener);
Should be:
应该:
searchView.setOnQueryTextListener(this);
If you have any problem let me know, I was with a similar problem today and read your question without answer.
如果您有任何问题,请告诉我,我今天遇到了类似的问题,并且在没有答案的情况下阅读了您的问题。