Android:具有默认建议的AutoCompleteTextView

Ken*_* Ma 7 android autocompletetextview

如何在用户输入任何内容之前显示AutoCompleteTextView的一些默认建议?即使创建扩展AutoCompleteTextView的自定义类,我也找不到这样做的方法.

我想显示常见输入值的建议,以防止用户输入.

有什么建议?

Ita*_*ana 5

您应该子类化AutoCompleteTextView和覆盖enoughToFilter()以一直返回true.之后你可以打电话performFiltering("",0)(这是一个受保护的功能,所以你可以通过你班上的公共功能导出这个电话).

像这样的东西:

public class ContactsAutoCompleteTextView extends AutoCompleteTextView {


    public ContactsAutoCompleteTextView(Context context) {
        super(context);
    }

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

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

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    public void temp() {
        performFiltering("",0);
    }
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*son -1

如果您不需要它是动态的,我会在资源中包含一个字符串数组,然后在即将查看 AutoCompleteTextView 时加载该数组。喜欢:

\n\n
 public class CountriesActivity extends Activity {\n     protected void onCreate(Bundle icicle) {\n         super.onCreate(icicle);\n         setContentView(R.layout.countries);\n\n         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,\n                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);\n         AutoCompleteTextView textView = (AutoCompleteTextView)\n                 findViewById(R.id.countries_list);\n         textView.setAdapter(adapter);\n     }\n\n     private static final String[] COUNTRIES = new String[] {\n         "Belgium", "France", "Italy", "Germany", "Spain"\n     };\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

可以在http://developer.android.com/reference/android/widget/AutoCompleteTextView.html上找到

\n\n

我已经做过几次的另一种允许它向用户学习的方法是使用 IE 的数据库连接和简单的游标。创建数据库时,您可以插入一些默认值。\nHere\xc2\xb4s 使用简单光标适配器的示例:http://androidcommunity.com/forums/f4/how-to-use-autocompletetextview-with-simplecursoradapter-15875/

\n\n

编辑1:

\n\n

在用户开始输入之前显示列表的一种想法是在 EditText 下面有一个简单的列表视图。不确定您是否可以调用 autocompletetextview 来显示建议,应该可以。也许您需要创建自己的 autocompletetextiew 类。

\n