编辑文本偏好设置是否可以自动完成?

7

是否可以将自动完成附加到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);
3个回答

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提供的源链接。

差不多了!我的自动完成框出现了,但是自动完成下拉框被截断了,并且显示在输入字段上方,下拉列表的下半部分没有显示。 - Chris Knight
实际上,我通过为下拉框高度硬编码一个值来修复了我在先前评论中提到的错误。 - Chris Knight

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

0

可能如果你子类化它并为其创建自己的视图,并将AutoCompleteTextView对象用作元素,它就会起作用,因为目前我不知道如何将简单的EditText更改为自动完成。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接