AutoCompleteTextView 구현과 리스트 클릭 이벤트
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
package exam.AutoComplete;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.inputmethod.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
public class AutoComplete extends Activity {
private String[] COUNTRIES = new String[] {
"asp.net",
"php",
"java",
"javascript",
"flex",
"actionscript",
"jsp"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AutoCompleteTextView autoComplete = (AutoCompleteTextView)findViewById(R.id.autoComplete);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
autoComplete.setAdapter(adapter);
// AutoCompleteTextView 자동완성 리스트에 클릭 이벤트
autoComplete.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
AutoCompleteTextView autoComplete = (AutoCompleteTextView)findViewById(R.id.autoComplete);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(autoComplete.getText());
// 열려있는 키패드 닫기
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(autoComplete.getWindowToken(), 0);
}
});
}
}
리스트 스타일을 바꿔보자.
레이아웃 xml파일을 새로 만들고 아래와 같이 작성
auto_complete_list.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="7pt"
android:textColor="#FF0000"
android:padding="6px"
/>
아래 소스 부분을
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);
새로 만든 레이아웃으로 변경
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.auto_complete_list, COUNTRIES);