在AutocompleteTextView中显示所有项目,而无需编写文本 [英] Show all items in AutocompleteTextView without writing text

查看:64
本文介绍了在AutocompleteTextView中显示所有项目,而无需编写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AutocompleteTextView,它可以正常工作.当我写一个单词时,它会显示相关的结果,但我想显示所有项目,而无需在AutocompleteTextView中写任何单词.我怎样才能做到这一点.

I have a AutocompleteTextView and it works fine. When I write a word it shows the relevant result but I want to show all items without writing any word in AutocompleteTextView. How can I do that.

推荐答案

您需要扩展AutoCompleteTextView,

You need to extend AutoCompleteTextView,

"当阈值小于或等于0时,阈值1为 已应用."

"When threshold is less than or equals 0, a threshold of 1 is applied.".

setThreshold

import android.content.Context;  
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;

public class InstantAutoComplete extends AutoCompleteTextView {

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

    public InstantAutoComplete(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
    }

    public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
        super(arg0, arg1, arg2);
    }

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

    @Override
    protected void onFocusChanged(boolean focused, int direction,
            Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
       if (focused && getFilter()!=null) {
        performFiltering(getText(), 0);
    }
    }

}

在xml

<AutoCompleteTextView ... /> to <your.namespace.InstantAutoComplete ... />

编辑1

创建一个名为InstantAutoComplete的新类,然后将此代码放入该类中.

Create new class named InstantAutoComplete then put this code into the class.

在布局xml中,使用此类

In your layout xml use this class like

然后在您的活动中找到此小部件(onCreate方法).

then find this widget in your actity (onCreate method).

看这个例子

这篇关于在AutocompleteTextView中显示所有项目,而无需编写文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆