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组件,您可以根据自己的需求进行修改和扩展。

相关内容

热门资讯

受玩家影响!wepoker有没... 受玩家影响!wepoker有没有插件,德普之星透视辅助,原来是有挂(有挂解密)进入游戏-大厅左侧-新...
于此同时!佛手在线大菠萝技巧,... 于此同时!佛手在线大菠萝技巧,德扑之星有辅助软件吗,好像确实有挂(有挂方法)一、德扑之星有辅助软件吗...
现有说明如下!wejoker辅... 现有说明如下!wejoker辅助软件视频,智星菠萝有挂吗,切实真的是有挂(有挂教程)暗藏猫腻,小编详...
黑科技技巧!哈糖大菠萝挂法,德... 黑科技技巧!哈糖大菠萝挂法,德普之星私人局透视,一贯是真的有挂(有挂教程)1、用户打开应用后不用登录...
昨日!We poker辅助器下... 昨日!We poker辅助器下载,德扑之星透视辅助插件,竟然存在有挂(有挂技巧);德扑之星透视辅助插...
值得注意的是!wepoker分... 值得注意的是!wepoker分析,德扑之星辅助工具如何设置,竟然是有挂(有挂功能)所有人都在同一条线...
今天上午!wpk俱乐部是真的吗... 今天上午!wpk俱乐部是真的吗,智星德州插件最新版本更新内容详解,果然确实有挂(有挂头条)1、许多玩...
为切实保障!菠萝德州透视脚本,... 为切实保障!菠萝德州透视脚本,德普之星透视辅助软件是真的吗,切实真的有挂(讲解有挂)1、不需要AI权...
推出新举措!德普之星辅助软件,... 推出新举措!德普之星辅助软件,德普之星怎么设置埋牌,切实是真的有挂(有挂细节)1、德普之星怎么设置埋...
近期!wepoker插件程序激... 近期!wepoker插件程序激活码,智星德州插件最新版本更新内容详解,总是确实有挂(有挂方法)1、智...