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

相关内容

热门资讯

现有关情况通报如下!sohoo... 现有关情况通报如下!sohoopoker辅助,aapoker真的假的(透视)解密教程(有挂分享)-哔...
透视能赢!wepoker私人局... 透视能赢!wepoker私人局透视方法,wepoker私局辅助(透视)教你教程(有挂辅助)-哔哩哔哩...
透视存在!wpk模拟器多开,w... 透视存在!wpk模拟器多开,wepoker怎么增加运气(透视)解密教程(有挂讲解)-哔哩哔哩1、用户...
透视存在!sohoo竞技联盟辅... 透视存在!sohoo竞技联盟辅助器,德普之星有透视辅助吗(透视)推荐教程(有挂方略)-哔哩哔哩1、上...
透视规律!hhpoker开挂教... 透视规律!hhpoker开挂教程,wepoker辅助透视(透视)总结教程(有挂秘笈)-哔哩哔哩1、不...
透视新版!拱趴大菠萝机器人,n... 透视新版!拱趴大菠萝机器人,newpoker脚本(透视)开挂教程(有挂猫腻)-哔哩哔哩1、拱趴大菠萝...
透视神器!wpk刷入池率脚本,... 透视神器!wpk刷入池率脚本,德州透视是真的吗(透视)推荐教程(的确有挂)-哔哩哔哩1、完成德州透视...
据了解!aapoker发牌逻辑... 据了解!aapoker发牌逻辑,wepoker可以开透视吗(透视)推荐教程(真实有挂)-哔哩哔哩1、...
现场直击!wepoker有辅助... 现场直击!wepoker有辅助吗,wepoker有没有透视方法(透视)科普教程(有挂教学)-哔哩哔哩...
透视黑科技!hhpoker怎么... 透视黑科技!hhpoker怎么防辅助,wepoker科技辅助器(透视)解密教程(今日头条)-哔哩哔哩...