AutoCompleteTextView列表 - 不显示单词字符串首先,然后是两个单词字符串。
创始人
2024-09-22 20:00:31
0

以下是一个使用AutoCompleteTextView实现的示例代码,其中列表中不显示以输入的字符串开头的单词,只显示包含两个单词的字符串。

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private AutoCompleteTextView autoCompleteTextView;

    private String[] words = {"apple", "banana", "cherry", "orange", "pear", "pineapple"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        autoCompleteTextView = findViewById(R.id.autoCompleteTextView);

        ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, words);
        autoCompleteTextView.setAdapter(adapter);

        autoCompleteTextView.setThreshold(2); // 设置输入最小长度为2

        autoCompleteTextView.setOnItemClickListener((parent, view, position, id) -> {
            String selectedWord = (String) parent.getItemAtPosition(position);
            autoCompleteTextView.setText(selectedWord); // 设置AutoCompleteTextView的文本为选中的单词
        });

        autoCompleteTextView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String input = s.toString();
                ArrayAdapter filteredAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_dropdown_item_1line);
                for (String word : words) {
                    if (word.contains(" ") && word.startsWith(input)) {
                        filteredAdapter.add(word);
                    }
                }
                autoCompleteTextView.setAdapter(filteredAdapter);
            }
        });
    }
}

在布局文件activity_main.xml中,添加一个AutoCompleteTextView组件:


在这个示例中,我们首先创建了一个包含一些单词的字符串数组。然后,我们创建了一个ArrayAdapter来适配AutoCompleteTextView,并将其设置为AutoCompleteTextView的适配器。我们还设置了输入的最小长度为2,以便在用户至少输入两个字符后才开始显示自动完成的下拉列表。

接下来,我们添加了一个TextWatcher,以在输入文本发生变化时实时过滤并更新AutoCompleteTextView的下拉列表。在afterTextChanged方法中,我们遍历单词数组,并将包含两个单词并以输入的字符串开头的单词添加到新的适配器中。然后,我们将这个适配器设置为AutoCompleteTextView的适配器,以更新下拉列表。

当用户从下拉列表中选择一个单词时,我们使用setOnItemClickListener监听器将选择的单词设置为AutoCompleteTextView的文本。

请注意,此示例中的布局文件仅包含一个AutoCompleteTextView组件,您可以根据自己的需求进行修改和扩展。

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...