AutoCompleteTextView란 네이버 구글 등 자동완성 검색 기능을 사용할 때 사용한다.
1. xml 만들기
<AutoCompleteTextView
android:id="@+id/auto_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="hint"
/>
위와 같이 만들어 준다.
android:completionThreshold="2"
copletionThreshold는 몇 개의 문자를 입력해야 자동완성 리스트가 나오는지 설정한다.
2. 항목 리스트 구성하기
val list = mutableListOf<String>("test1", "test2", "test3")
3. 어댑터 등록
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, wordList)
auto_tv.setAdapter(adapter)
결과 화면
completionThreshold를 1로 설정해 줬을때의 화면이다. 위와같이 나오면 성공이다.
ps. AAC를 이용하여 mvvm을 설정하였을때의 설정법
1. 어댑터 선언
val adapter = context?.let { ArrayAdapter<String>(it, android.R.layout.simple_dropdown_item_1line, list) }
화면을 fragment로 구성하였기 때문에 context가 null이 아닐에 어댑터를 걸어주는 방식으로 진행한다
2. 어댑터 설정
DataBinding.AutoTv.setAdapter(adapter)
데이터바인딩 하는 뷰에서 가져와서 어댑터를 설정해준다.
다른것은 위와 동일하다.
728x90
'개발 공부 기록 > 04. Android' 카테고리의 다른 글
[Android][Kotlin]fragment 에서 Notification으로 알림주기 (0) | 2021.02.15 |
---|---|
[Kotlin] 데이터가 리스트 안에 존재하는지 찾는법 (0) | 2020.12.29 |
[Android][Kotlin] SharedPreferences 이용하여 자동로그인 구현하기 (0) | 2020.12.01 |
[PHP] php에서 fcm 으로 push 메시지 보내기 (0) | 2020.11.27 |
안드로이드 리사이클러뷰 스크롤 지정하기가 안될 때 (0) | 2020.11.26 |