2010-07-24 55 views
7

是否有可能使用AutoComplete附加到EditTextPreference?可能自动完成EditTextPreference?

我知道要附加一个ID到一个元素,但我很难弄清楚如何将ArrayAdapter附加到首选项字段。

这是错误的,但它尽可能地接近。

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); 

回答

0

也许,如果你继承它,使自己的观点为,并使用AutoCompleteTextView对象作为元素,它将工作,因为目前我没有看到一个简单的EditText如何可以改变自动完成。

8

下面是我通过研究EditTextPreference.java源代码实施的解决方法。

基本上,您需要子类EditTextPreference并重写绑定到对话框时。此时,您可以检索EditText,复制其值,并将其从其父视图组中删除。然后你注入你的Autocompletetextview并挂接它的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

在我看来,必须有一个“容易”的方式来做到这一点比黑客进入EditTextPreference类,并与视图搞乱。这是我的解决方案,因为AutoCompleteTextView扩展了EditText,我只需要重写直接调用其常量EditText对象的EditTextPreference方法。

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); 
     } 
    } 
} 
} 

感谢Brady连接到源代码。

+0

差不多!我得到我的自动完成框出现,但自动完成下拉框被切断,并显示在输入字段上方,而下拉列表的下半部分未显示。 – 2011-08-09 22:14:28

+0

其实,我可以通过硬编码下拉框高度的值来修复以前评论中的错误。 – 2011-08-10 07:43:14