Android:带有默认建议的 AutoCompleteTextView

发布于 2024-11-19 03:51:08 字数 150 浏览 3 评论 0原文

在用户输入任何内容之前,如何显示 AutoCompleteTextView 的一些默认建议?即使创建扩展 AutoCompleteTextView 的自定义类,我也找不到方法来执行此操作。

我想显示常见输入值的建议,以节省用户打字的时间。

有什么建议吗?

How do I show some default suggestions for AutoCompleteTextView before the user type anything? I cannot find a way to do this even with creating a custom class that extends AutoCompleteTextView.

I want to show suggestions for common input values to save the user from typing.

Any suggestions?

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

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

发布评论

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

评论(3

勿挽旧人 2024-11-26 03:51:08

您应该子类化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);
    }
}

You should subclass AutoCompleteTextView and override enoughToFilter() to return true all the time. After that you can call performFiltering("",0) (it's a protected function, so you can export this call via a public function in your class).

Something like that:

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);
    }
}
紫南 2024-11-26 03:51:08

Itay Kahana的回答确实是正确的。我唯一要添加的是,不要创建 temp() 函数,而是重写 onFocusChanged 函数。我个人使用了以下内容:

 @Override
        protected void onFocusChanged (boolean focused, int direction, Rect previouslyFocusedRect) {
            if(focused)
                performFiltering("", 0);
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
        }

Itay Kahana's answer is indeed correct. The only thing I would add is that instead of creating a temp() function, to override the onFocusChanged function. Personally I used the following:

 @Override
        protected void onFocusChanged (boolean focused, int direction, Rect previouslyFocusedRect) {
            if(focused)
                performFiltering("", 0);
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
        }
灯角 2024-11-26 03:51:08

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

 public class CountriesActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.countries);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain"
     };
 }

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

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

编辑1:

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

If you dont need it to be dynamic I would go by having a string array in the resources, and then just load the array when the AutoCompleteTextView is about to be viewed. Like:

 public class CountriesActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.countries);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain"
     };
 }

Which can be found on http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Another way which I have done a couple of times which allows it to learn from the user is o use a database connection with IE a simple cursor. When you create the db you could insert some default values.
Here´s an example with using simple cursor adapter: http://androidcommunity.com/forums/f4/how-to-use-autocompletetextview-with-simplecursoradapter-15875/

Edit 1:

One idea to show the list before the user starts type is to have a simple listview below the EditText. Not sure if you could call the autocompletetextview to show the suggestions, should be possible somehow. Perhaps you need to create your own autocompletetextiew class.

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