显示 AutocompleteTextView 中的所有项目而不写入文本 [英] Show all items in AutocompleteTextView without writing text

查看:32
本文介绍了显示 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天全站免登陆