AutoCompleteTextView
这个控件其实很简单,举个大家就知道他是干什么用的了,打开手机的通讯录,在搜索框中输入姓名,当输入没有完成时,他就会给你列出自动匹配的联系人。AutoCompleteTextView就是这个功能,下面我们来看下AutoCompleteTextView如何使用:(在这说明一下AutoCompleteTextView的使用也是同ArrayAdapter一起)
- 首先定义Adapter中的数据:
1 |
private String[] mData = {"layout_linear", "layout_relative", "layout_frame", "Layout_table", "view_text", "view_image"}; |
- 在Activity的布局中添加AutoCompleteTextView,并在Activity中获得AutoCompleteTextView的对象:
1 2 3 4 5 |
<AutoCompleteTextView android:id="@+id/autocompletetextview_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入要查找内容"/> |
- 在Activity中调用Adapter适配器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class MainActivity extends Activity { private AutoCompleteTextView mAutoCompleteTextViewContent; private ArrayAdapter<String> mAdapter; private String[] mData = {"layout_linear", "layout_relative", "layout_frame", "Layout_table", "view_text", "view_image"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAutoCompleteTextViewContent = (AutoCompleteTextView) findViewById(R.id.autocompletetextview_content); mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mData); mAutoCompleteTextViewContent.setAdapter(mAdapter); Log.d("data", "" + mAutoCompleteTextViewContent.getText()); } } |
结果如下: