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()); } } |
結果如下: