您应该尝试使用在 ActionBar中使用 SearchView 的 android 功能
还有它的兼容性提供了Collapsable SearchView
以下是您可以从ArrayList
试用中实现搜索的方式:
public class SearchActivity extends ListActivity
{
private ArrayAdapter<String> m_arrayAdapter;
private SearchView m_searchView;
@Override
protected void onCreate(Bundle p_savedInstanceState)
{
super.onCreate(p_savedInstanceState);
// setContentView(R.layout.search);
m_arrayAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, m_items);
setListAdapter(m_arrayAdapter);
}
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
// MenuInflater inflater = getMenuInflater();
// inflater.inflate(R.menu.activity_main, menu);
getMenuInflater().inflate(R.menu.activity_main, menu);
m_searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
m_searchView.setSubmitButtonEnabled(true);//Set the submit button on search view.
// Associate searchable configuration with the SearchView
// SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
// SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
m_searchView.setOnQueryTextListener(new OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String p_query)
{
m_arrayAdapter.getFilter().filter(p_query);
return true;
}
@Override
public boolean onQueryTextChange(String p_newText)
{
m_arrayAdapter.getFilter().filter(p_newText);
return false;
}
});
return true;
};
String[] m_items = new String[] { "China", "India", "United States", "Indonesia", "Brazil",
"Pakistan", "Nigeria", "Bangladesh", "Russia", "Japan", "Mexico", "Philippines",
"Vietnam", "Ethiopia", "Egypt", "Germany", "Iran", "Turkey",
"Democratic Republic of the Congo", "Thailand", "France", "United Kingdom", "Italy",
"South Africa", "Myanmar", "South Korea", "Colombia", "Spain", "Ukraine", "Tanzania",
"Kenya", "Argentina", "Poland", "Algeria", "Canada" };
}