我想构建一个具有sherlock操作栏和内部搜索视图的应用程序.但是,我希望此searchview具有autocompleteTextView具有的自动完成功能.是否有单独使用searchview的直接实现?或者我应该在操作栏内使用autocompleteTextView来执行此操作?我发现了几个帖子,但没有一个人在帮助我.这些帖子只讨论创建autocompleteTextView,但我希望搜索视图具有此功能.知道如何做到这一点?
解决方法
为此,我使用AutoCompleteTextView创建一个布局,并将其添加到ActionBar中,在ActionBar中调用自定义布局.
之后,我使用android.R.layout.simple_dropdown_item_1line创建了适配器.在AutoCompleteTextView中设置它.
检查以下代码:
package com.example.testapp; import android.annotation.TargetApi; import android.app.ActionBar; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; @TargetApi(11) 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>
好运!