获取操作栏中搜索视图的下拉列表

时间:2015-09-23 12:06:55

标签: android search android-actionbar

你们能给我建议吗?如何编辑此搜索视图,向我显示autoCompleteTextView中的下拉列表?需要它来提供mathing项目,接下来是onClick

 ...
 final MenuItem searchItem = menu.findItem(R.id.search);
 setupSearchView(searchItem);
 ...

 private void setupSearchView(MenuItem searchItem) {
 mSearchView = (SearchView) searchItem.getActionView();
 mSearchView.setQueryHint(getString(R.string.product_id));
 mSearchView.setQuery(mSearchQuery, false);
 mSearchView.setIconified(!mExpandSearch);
 final int searchIconResId = getResources().getIdentifier("android:id/search_button", null, null); 
 final ImageView searchIcon = (ImageView) mSearchView.findViewById(searchIconResId);
    searchIcon.setImageResource(R.drawable.ic_action_search);  //
 ...

xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/search"
    android:showAsAction="always"
    android:icon="@drawable/ic_action_search"
    android:title="@string/search"
    android:actionViewClass="android.widget.SearchView" />

</menu>

1 个答案:

答案 0 :(得分:1)

您可以使用AutoCompleteTextView

代替SearchView
<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>

并且在活动中

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, autocompletelistvalues);
    AutoCompleteTextView textView = (AutoCompleteTextView) v
            .findViewById(R.id.editText1);
    textView.setAdapter(adapter);

包含此功能以动态过滤

 textView.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, ....){
            //update your List based on CharSequnce entered  
        textView.setAdapter(filteredadapter);
         notifyDataSetChanged();
        }
    });