我试图让搜索框在Action Bar Sherlock上运行。
这是我的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();
}
}
这是我的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
}
}
根本没有成功。就像在,当我在Prelocation Activity上输入内容并按回车键时,没有搜索任何内容。我是否必须将其视为EditText
并为其编写文本侦听器,然后调用geoCoder
并获取位置或是否有更明智的方法来实现它?
答案 0 :(得分:38)
支持库v7: http://developer.android.com/tools/support-library/features.html#v7-appcompat
Google现在支持ActionBar与Android 2.1(API 7)的兼容性。
转换很容易,因为方法名称相同和/或非常相似。
使用资源添加支持库: http://developer.android.com/tools/support-library/setup.html#libs-with-res
您的舱单:AndroidManifest.xml
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="17" />
您的菜单: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"/>
以下是如何在Android中使用ActionBarSherlock的标准SearchView和SearchManager!我使用此代码并正常工作。我在Android 2.3(API 10) - Android 4.3(API 18)上测试了这个。
优秀的教程和文档:
请记住:
使用ActionBarSherlock进行自定义搜索(最低API 7)
SearchView with ActionBarSherlock(min.API 8)
您的菜单: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"/>
您的活动: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);
}
如果这对你有用,请告诉我,如果你需要帮助,请告诉我们!
答案 1 :(得分:26)
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
}
};
答案 2 :(得分:3)
这里有一个类似的问题,我有问题要分配适配器。
How to implement search widget with a listview using SherlockActionbar?
我希望它会有所帮助,因为我遇到了同样的问题。
要使用适配器,最好的方法是该类实现SearchView.OnQueryTextListener,然后您不必创建内部类,您将拥有适配器。
在您的代码中将是:
public class MainActivity extends SherlockListActivity implements SearchView.OnQueryTextListener
然后在课堂上你必须定义方法。适配器将是您的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;
}
在您要设置的行中:
searchView.setOnQueryTextListener(queryTextListener);
应该是:
searchView.setOnQueryTextListener(this);
如果您有任何问题请告诉我,我今天遇到了类似的问题并且无需回答即可阅读您的问题。