자동 완성 기능은 AutoCompleteTextView를 사용한다.


1
2
3
4
5
    <AutoCompleteTextView
        android:id="@+id/autocomplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"/>
cs


AutoCompleteTextView에 자동 완성 리스트를 연결하여 주면 자동 완성이 표시된다.


1
2
3
4
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this
                                    android.R.layout.simple_dropdown_item_1line, androids);
    textView.setAdapter(adapter);
cs


ArrayAdapter<String>을 상속받는 Adapter 클래스를 만들어서 쉽게 제어도 가능하다.


1
2
3
4
5
6
7
8
    List<String> androids = new ArrayList<>();
    androids.add("좋아하는 색은 빨간색");
    androids.add("좋아하는 색은 파란색");
    androids.add("좋아하는 색은 노란색");
 
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    SuggestAdapter adapter = new SuggestAdapter(this, androids);
    textView.setAdapter(adapter);
cs


SuggestAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class SuggestAdapter extends ArrayAdapter<String> implements SpinnerAdapter {
    private LayoutInflater mInflator;
    private List<String> mItems;
 
    SuggestAdapter(Context context, List<String> objects) {
        super(context, R.layout.item_suggest, objects);
        mInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mItems = objects;
    }
 
    @Override
    public int getCount() {
        return mItems.size();
    }
 
    @Override
    public String getItem(int position) {
        return mItems.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
 
        if (convertView == null) {
            convertView = mInflator.inflate(R.layout.item_suggest, nullfalse);
            holder = new ViewHolder();
            holder.textView = (TextView) convertView.findViewById(R.id.spinnerText);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
 
        holder.textView.setText(mItems.get(position));
 
        return convertView;
    }
 
    private class ViewHolder {
        TextView textView;
    }
}
cs


item_suggest.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:id="@+id/spinnerText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/FlatDarkRed"
        android:padding="@dimen/padding_large"
        android:textColor="@color/FlatLightBlue"
        android:textSize="18sp" />
</LinearLayout>
cs


'Android' 카테고리의 다른 글

YouTube Android Player API - Download  (0) 2019.03.20
[Android] DrawerLayout 사용하기  (0) 2019.03.19
[Android] EditText 사용하기  (0) 2019.03.13
[Android] SeekBar 사용  (0) 2019.03.12
[Android] 라디오 버튼(RadioButton) 사용  (0) 2019.03.11

+ Recent posts