【问题标题】:AutoCompleteTextView filtering multiple words over the same stringAutoCompleteTextView 过滤同一字符串上的多个单词
【发布时间】:2018-05-13 10:47:54
【问题描述】:

有没有办法让 AutoCompleteTextView 通过分隔词过滤建议?该控件的正常行为是按您编写的整个字符串进行过滤。我给你举个例子。我们在适配器上有下一句话(它们将是建议):

String[] words = {"foo bar bla bla ", "foo bla bla bar"};

如果我们在输入"foo bar" 处写入,则建议将是单词数组"foo bar bla bla" 的索引0,因为文本匹配为输入的子字符串。

我的问题是:有没有办法将输入 "foo bar" 过滤为单独的单词? (在这种情况下,按空格,但它可以是逗号、星号或其他),使 AutoCompleteTextView 过滤匹配"foo" "bar" 的建议。

对于我说的例子,它会将数组的两个字符串都显示为建议,因为它们包含 "foo""bar" 作为子字符串。

谢谢你,对不起我的英语不好。

最好的问候。

【问题讨论】:

    标签: android filtering autocompletetextview


    【解决方案1】:

    试试下面的代码:

           AutoCompleteTextView text;
           MultiAutoCompleteTextView text1;
           String[] words = {"foo bar bla bla ", "foo bla bla bar"};
    
               @Override
               protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  setContentView(R.layout.activity_main);
    
                  text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
                  text1=(MultiAutoCompleteTextView)findViewById(R.id.multiAutoCompleteTextView1);
    
                  ArrayAdapter adapter = new 
                     ArrayAdapter(this,android.R.layout.simple_list_item_1,words);
    
                  text.setAdapter(adapter);
                  text.setThreshold(1);
    
                  text1.setAdapter(adapter);
                  text1.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
               }
    

    或 请参考Android - Auto Complete

    【讨论】:

    • 我已经尝试过 MultiAutoCompleteTextView,但它并没有按照我想要的方式工作。它通过标记分隔文本,但只查找最后一个标记的建议。我想要实现的行为是通过过滤 all 标记来显示建议,而不仅仅是最后一个。我做了一个测试,你可以在this image 中看到它是如何只过滤最后一个令牌"red"
    猜你喜欢
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2013-07-12
    • 2016-02-23
    相关资源
    最近更新 更多