2010-07-24 11 views
7

¿Es posible tener una EditTextPreference con Autocompletar adjunto?Posible autocompletar una EditTextPreference?

Sé que puedo adjuntar uno a un elemento con una identificación, pero tengo problemas para averiguar cómo conectar el ArrayAdapter al campo de preferencia.

Esto está mal, pero es lo más cerca que puedo llegar.

final String[] TEAMS = getResources().getStringArray(R.array.teams); 
AutoCompleteTextView EditTextPreference = (AutoCompleteTextView) findViewById(R.id.editTextPrefTeam);  
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, TEAMS); 
EditTextPreference.setAdapter(adapter); 

Respuesta

0

Probablemente si la subclase, y hacer su propio punto de vista y para eso utiliza el objeto AutoCompleteTextView como elemento que va a trabajar, ya que actualmente no veo cómo un EditarTexto simple puede ser cambiado para autocompletar.

8

Aquí hay una solución que implementé estudiando el código fuente EditTextPreference.java.

Esencialmente es necesario crear una subclase EditTextPreference y anular cuando se une al cuadro de diálogo. En este punto, puede recuperar EditText, copiar sus valores y eliminarlo de su grupo de vista principal. Luego, inyecta su vista Autocompletar de texto y conecta su Arrayadapter.

public class AutoCompleteEditTextPreference extends EditTextPreference 
{ 
    public AutoCompleteEditTextPreference(Context context) 
    { 
     super(context); 
    } 

    public AutoCompleteEditTextPreference(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 

    public AutoCompleteEditTextPreference(Context context, AttributeSet attrs, 
     int defStyle) 
    { 
     super(context, attrs, defStyle); 
    }  

    /** 
    * the default EditTextPreference does not make it easy to 
    * use an AutoCompleteEditTextPreference field. By overriding this method 
    * we perform surgery on it to use the type of edit field that 
    * we want. 
    */ 
    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, [LIST OF DATA HERE]); 
     mACTV.setAdapter(adapter); 

     // add the new view to the layout 
     vg.addView(mACTV); 
    } 

    /** 
    * Because the baseclass does not handle this correctly 
    * we need to query our injected AutoCompleteTextView for 
    * the value to save 
    */ 
    protected void onDialogClosed(boolean positiveResult) 
    { 
     super.onDialogClosed(positiveResult); 

     if (positiveResult && mACTV != null) 
     {   
      String value = mACTV.getText().toString(); 
      if (callChangeListener(value)) { 
       setText(value); 
      } 
     } 
    } 

    /** 
    * again we need to override methods from the base class 
    */ 
    public EditText getEditText() 
    { 
     return mACTV; 
    } 

    private AutoCompleteTextView mACTV = null; 
    private final String TAG = "AutoCompleteEditTextPreference"; 
} 
8

Me parecía que tenía que haber una manera "fácil" de lograr esto que por la piratería en la clase EditTextPreference y jugar con la vista. Esta es mi solución, dado que AutoCompleteTextView extiende EditText, solo tuve que anular los métodos EditTextPreference que llaman directamente a su objeto constante EditText.

public class AutoCompletePreference extends EditTextPreference { 

private static AutoCompleteTextView mEditText = null; 

public AutoCompletePreference(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mEditText = new AutoCompleteTextView(context, attrs); 
    mEditText.setThreshold(0); 
    //The adapter of your choice 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, COUNTRIES); 
    mEditText.setAdapter(adapter); 
} 
private static final String[] COUNTRIES = new String[] { 
    "Belgium", "France", "Italy", "Germany", "Spain" 
}; 

@Override 
protected void onBindDialogView(View view) { 
    AutoCompleteTextView editText = mEditText; 
    editText.setText(getText()); 

    ViewParent oldParent = editText.getParent(); 
    if (oldParent != view) { 
     if (oldParent != null) { 
      ((ViewGroup) oldParent).removeView(editText); 
     } 
     onAddEditTextToDialogView(view, editText); 
    } 
} 

@Override 
protected void onDialogClosed(boolean positiveResult) { 
    if (positiveResult) { 
     String value = mEditText.getText().toString(); 
     if (callChangeListener(value)) { 
      setText(value); 
     } 
    } 
} 
} 

Gracias a Brady por enlazar a la fuente.

+0

Casi! Aparece el cuadro de autocompletar, pero el cuadro desplegable autocompletar se corta y se muestra encima del campo de entrada con la mitad inferior de la lista desplegable sin mostrar. –

+0

En realidad, pude corregir mi error a partir de comentarios previos codificando un valor para la altura del cuadro desplegable. –

Cuestiones relacionadas