最近項目開發有數據聯動功能需求,正好看到了AutoCompleteTextView這個控件,這裏記錄一下用法。
<AutoCompleteTextView android:id="@+id/actv_album_input_lesson_name" android:layout_width="match_parent" android:layout_height="55dp" android:completionThreshold="1"/>
這個控件只需要注意android:completionThreshold這個屬性即可,顧名思義,是設置用戶在輸入幾個字符的時候出現提示,我這裏設置的是輸入一個字符。
autoCompleteTextView=findViewById(R.id.actv_album_input_lesson_name);
autoCompleteTextView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line ,lessonName_list));
在java代碼中傳入一個字符串數組即可,樣式可以根據業務需求重寫adapter或者直接使用官方提供的基礎樣式。