Android操作栏SearchView作为自动完成?
我正在使用操作栏中的SearchView。 我想在search视图上使用自动完成function从数据库中获取结果。
这可能吗? 或者我需要使用自定义文本框,然后添加自动完成?
我使用自定义的AutoCompleteTextView并将其添加到ActionBar中。
public class MainActivity extends Activity { private static final String[] COUNTRIES = new String[] { "Belgium", "France", "France_", "Italy", "Germany", "Spain" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayShowCustomEnabled(true); // actionBar.setDisplayShowTitleEnabled(false); // actionBar.setIcon(R.drawable.ic_action_search); LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflator.inflate(R.layout.actionbar, null); actionBar.setCustomView(v); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); AutoCompleteTextView textView = (AutoCompleteTextView) v .findViewById(R.id.editText1); textView.setAdapter(adapter); } }
和你的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Action Bar:" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#FFFFFF" /> <AutoCompleteTextView android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:imeOptions="actionSearch" android:inputType="textAutoComplete|textAutoCorrect" android:textColor="#FFFFFF" > <requestFocus /> </AutoCompleteTextView> </LinearLayout>
编辑:
请检查这个和这个链接可能会帮助你。 代码在这里。
所以我只需要为v7版本做这个,并且惊讶地发现我不能简单地用一个ArrayAdapter来设置适配器。
我不想使用一个股票AutoCompleteTextView(作为顶级评论者在这里),因为那么你错过了一些SearchView时髦的function,如小search图标和Xbutton。
所以我扩展了SearchView并得到这个:
public class ArrayAdapterSearchView extends SearchView { private SearchView.SearchAutoComplete mSearchAutoComplete; public ArrayAdapterSearchView(Context context) { super(context); initialize(); } public ArrayAdapterSearchView(Context context, AttributeSet attrs) { super(context, attrs); initialize(); } public void initialize() { mSearchAutoComplete = (SearchAutoComplete) findViewById(android.support.v7.appcompat.R.id.search_src_text); this.setAdapter(null); this.setOnItemClickListener(null); } @Override public void setSuggestionsAdapter(CursorAdapter adapter) { // don't let anyone touch this } public void setOnItemClickListener(OnItemClickListener listener) { mSearchAutoComplete.setOnItemClickListener(listener); } public void setAdapter(ArrayAdapter<?> adapter) { mSearchAutoComplete.setAdapter(adapter); } public void setText(String text) { mSearchAutoComplete.setText(text); } }
你可以在你的菜单xml中使用这个ActionBar,像这样:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/action_search" android:icon="@android:drawable/ic_menu_add" android:title="TITLE" app:actionViewClass="com.yourpackage.ArrayAdapterSearchView" app:showAsAction="ifRoom|collapseActionView"/> </menu>
您可能还想要将自动完成列表的单击function添加(例如,将文本设置为EditText):
MenuItem searchItem = menu.findItem(R.id.action_search); final ArrayAdapterSearchView searchView = (ArrayAdapterSearchView) MenuItemCompat.getActionView(searchItem); searchView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { searchView.setText(adapter.getItem(position).toString()); } });
这里是一个普通的旧的android.widget.SearchView类似的版本:
public class ArrayAdapterSearchView extends SearchView { private AutoCompleteTextView mSearchAutoComplete; public ArrayAdapterSearchView(Context context) { super(context); initialize(); } public ArrayAdapterSearchView(Context context, AttributeSet attrs) { super(context, attrs); initialize(); } public void initialize() { mSearchAutoComplete = (AutoCompleteTextView) findViewById(getResources().getIdentifier("android:id/search_src_text", null, null)); setAutoCompleSuggestionsAdapter(null); setOnItemClickListener(null); } @Override public void setSuggestionsAdapter(CursorAdapter adapter) { throw new UnsupportedOperationException("Please use setAutoCompleSuggestionsAdapter(ArrayAdapter<?> adapter) instead"); } public void setOnItemClickListener(AdapterView.OnItemClickListener listener) { mSearchAutoComplete.setOnItemClickListener(listener); } public void setAutoCompleSuggestionsAdapter(ArrayAdapter<?> adapter) { mSearchAutoComplete.setAdapter(adapter); } public void setText(String text) { mSearchAutoComplete.setText(text); } }
这是一个使用CursorAdapter
的替代方法:
ExampleActivity.java
private Menu menu; @Override @TargetApi(Build.VERSION_CODES.HONEYCOMB) public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.example, menu); this.menu = menu; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView search = (SearchView) menu.findItem(R.id.search).getActionView(); search.setSearchableInfo(manager.getSearchableInfo(getComponentName())); search.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextChange(String query) { loadHistory(query); return true; } }); } return true; } // History @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void loadHistory(String query) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { Cursor cursor = db.rawQuery("SELECT * FROM items", null); // Example database query SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); final SearchView search = (SearchView) menu.findItem(R.id.search).getActionView(); search.setSearchableInfo(manager.getSearchableInfo(getComponentName())); search.setSuggestionsAdapter(new ExampleAdapter(this, cursor, items)); } }
现在您需要创build一个从CursorAdapter
扩展的适配器:
ExampleAdapter.java
public class ExampleAdapter extends CursorAdapter { private List<String> items; private TextView text; public ExampleAdapter(Context context, Cursor cursor, List<String> items) { super(context, cursor, false); this.items = items; } @Override public void bindView(View view, Context context, Cursor cursor) { text.setText(cursor.getString(cursor.getColumnIndex("text"))); // Example column index } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.item, parent, false); text = (TextView) view.findViewById(R.id.text); return view; } }
请注意:导入CursorAdapter
不要导入Android支持版本,而是导入标准的android.widget.CursorAdapter
。
该适配器还将需要一个自定义布局:
RES /布局/ item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/item" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
您现在可以通过向布局添加其他文本或图像视图并使用适配器中的数据填充它们来自定义列表项目。 现在你需要一个SearchView菜单项:
RES /菜单/的example.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/search" android:title="@string/search" android:showAsAction="ifRoom" android:actionViewClass="android.widget.SearchView" /> </menu>
然后创build一个可search的configuration:
RES / XML / searchable.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android" android:label="@string/search" android:hint="@string/search" > </searchable>
最后,在清单文件中的相关活动标签内添加:
AndroidManifest.xml中
<intent-filter> <action android:name="android.intent.action.SEARCH" /> </intent-filter> <meta-data android:name="android.app.default_searchable" android:value="com.example.ExampleActivity" /> <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
请注意:示例中使用的@string/search
string应该在values / strings.xml中定义,也不要忘记为您的项目更新对com.example
的引用。
以下是原始教程供参考:
http://tpbapp.com/android-development/android-action-bar-searchview-tutorial
是的,这是可能的。 为你的build议build立一个表(如在SQLiteDatabase中),并用必要的列格式化表。
看到这个链接
我也面临这个问题的自动完成search视图,并修复它没有使用任何额外的布局和自动完成文本视图,有一个名为SearchAutoComplete的类。 我用它来实现自动完成function,只是把一个简单的列表适配器,其中包含项目的ArrayList与search视图build议。 将适配器设置为您的SearchAutoComplete并自动完成function将工作下面是我的代码。 请记住,您不必添加任何自定义布局。 只需将您的onCreateOptionsMenu(…)方法replace为我的代码:
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); MenuItem searchItem = menu.findItem(R.id.search); SearchView mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); SearchAutoComplete searchAutoComplete = (SearchAutoComplete) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, itemArrayList); searchAutoComplete.setAdapter(adapter); SearchManager searchManager = (SearchManager) getSystemService(this.SEARCH_SERVICE); mSearchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName())); return true; }
点击build议的项目,这个项目应该出现在search视图中,所以在你的适配器设置为onCreateOptionmenu(…)方法内的searchAutoComplete之后,添加下面的代码
searchAutoComplete.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub String searchString=(String)parent.getItemAtPosition(position); searchAutoComplete.setText(""+searchString); Toast.makeText(MainActivity.this, "you clicked "+searchString, Toast.LENGTH_LONG).show(); } });
你可以通过类似于CursorAdapter来做到这一点:
MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); searchView.setSuggestionsAdapter(adapter);