'안드로이드 설정화면'에 해당되는 글 1건

  1. 2013.10.20 [Android] - 안드로이드 Preference AutoCompleteEditText

[Android] - 안드로이드 Preference AutoCompleteEditText

Android Java 2013. 10. 20. 14:30
반응형

AutoCompletePreference.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


<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="setting_activity_top_title"

<com.example.android.AutoCompletePreference
             android:key="profile_form_country"
             android:title="@string/profile_form_country" />
        
   </PreferenceCategory>


반응형
: