[Android] - 안드로이드 Preference AutoCompleteEditText
Android Java 2013. 10. 20. 14:30AutoCompletePreference.java
package com.example.android;
import android.content.Context;
import android.preference.EditTextPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
public class AutoCompletePreference extends EditTextPreference {
private static final String[] COUNTRIES = new String[] { "가나다라", "마바사", "아자차카", "타파하", "ABC" };
public AutoCompletePreference (Context context) {
super(context);
}
public AutoCompletePreference (Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoCompletePreference (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
// find the current EditText object
final EditText editText = (EditText)view.findViewById(android.R.id.edit);
// copy its layout params
LayoutParams params = editText.getLayoutParams();
ViewGroup vg = (ViewGroup)editText.getParent();
String curVal = editText.getText().toString();
// remove it from the existing layout hierarchy
vg.removeView(editText);
// construct a new editable autocomplete object with the appropriate params
// and id that the TextEditPreference is expecting
mACTV = new AutoCompleteTextView(getContext());
mACTV.setLayoutParams(params);
mACTV.setId(android.R.id.edit);
mACTV.setText(curVal);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
mACTV.setAdapter(adapter);
// add the new view to the layout
vg.addView(mACTV);
}
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult && mACTV != null) {
String value = mACTV.getText().toString();
if (callChangeListener(value)) {
setText(value);
}
}
}
public EditText getEditText() {
return mACTV;
}
private AutoCompleteTextView mACTV = null;
private final String TAG = "AutoCompleteEditTextPreference";
}
Preference.xml
'Android Java' 카테고리의 다른 글
[Android] - 안드로이드 버전 코드 추출방법 (0) | 2015.08.01 |
---|---|
[Android] - 안드로이드 DP -> Px 계산 Java Code (0) | 2013.12.29 |
[Android] - 안드로이드 SharedPreferences Service 에서 값 동기화 (0) | 2013.09.25 |
[Android] - 안드로이드 웹뷰 Post 데이터 전송 (0) | 2013.09.24 |
[Android] - 안드로이드 반복실행, Android TimerTask (0) | 2013.09.12 |