可以自动完成 EditTextPreference 吗?

发布于 2024-09-11 02:57:16 字数 517 浏览 4 评论 0原文

是否可以有一个带有自动完成功能的 EditTextPreference 附加到它?

我知道如何将一个 ArrayAdapter 附加到具有 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);

Is it possible to have an EditTextPreference with AutoComplete attached to it?

I know ho to attach one to an element with an id, but am having trouble figure out how to attach the ArrayAdapter to the preference field.

This is wrong, but it's as close as I can get.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吻风 2024-09-18 02:57:16

在我看来,必须有一种“更简单”的方法来完成此任务,而不是侵入 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);
        }
    }
}
}

感谢布雷迪链接到来源。

It seemed to me there had to be an "easier" way to accomplish this than by hacking into the EditTextPreference class and messing with the view. Here's my solution, since AutoCompleteTextView extends EditText, I only had to override the EditTextPreference methods that call their constant EditText object directly.

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

Thanks to Brady for linking to the source.

雄赳赳气昂昂 2024-09-18 02:57:16

这是我通过研究 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";
}

Here's a workaround I implemented by studying the EditTextPreference.java source code.

Essentially you need to subclass EditTextPreference and override when it binds to the dialog. At this point you can retrieve the EditText, copy it's values, and remove it from its parent view group. Then you inject your Autocompletetextview and hook up it's 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";
}
蓝天白云 2024-09-18 02:57:16

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

Probably if you subclass it and make your own view for that and use the AutoCompleteTextView object as element it will work, as currently I don't see how a simple EditText can be changed to autocomplete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文