본문 바로가기

프로그래밍/안드로이드

[안드로이드] 자동완성(Atuo Complete)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="아래 에디트에 단어를 입력하시오."
/>
<AutoCompleteTextView
android:id="@+id/autoedit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"
android:completionHint="목록에서 원하는 단어를 탭하십시오."
/>
</LinearLayout>


completionThreshold="1" : 몇 글자를 입력했을 떄 후보목록을 보여줄 것인가를 지정.

한글의 경우 1, 영문의 경우 2글자 정도가 적당하다.


completionHInt : 후보 목록 아래쪽에 표시할 도움말 문자열을 지정한다.



public class AutoComplete extends Activity {
String[] arWords = new String[] {
"가구", "가로수", "가방", "가슴", "가치", "가훈", "나그네", "다리미",
"above", "about", "absolute", "access", "activity", "adjust"
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete);


        // 후보 문자열로 어댑터를 생성
ArrayAdapter<String> adWord = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, arWords);
AutoCompleteTextView autoEdit = (AutoCompleteTextView)
findViewById(R.id.autoedit);
autoEdit.setAdapter(adWord); //
}
}