Android上的editText自动建议
创始人
2024-10-10 03:01:17
0

在这里,我们可以使用AutoCompleteTextView来实现这个功能,它是一个扩展了EditText类的控件,允许我们在文本输入时自动提示、完成功能,这个控件使用一个Adapter来为它提供建议列表。

首先,我们需要在布局中定义AutoCompleteTextView控件:


然后,在Activity中,我们需要为这个控件设置Adapter,最好是通过异步任务来加载建议数据:

public class MainActivity extends AppCompatActivity {
    private AutoCompleteTextView autoCompleteTextView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // 获取AutoCompleteTextView控件
        autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
 
        // 创建一个Adapter
        ArrayAdapter adapter = new ArrayAdapter(this,
                android.R.layout.simple_dropdown_item_1line, new String[] { "Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape", "Kiwi", "Lemon", "Mango", "Orange", "Pear", "Quince", "Raisin", "Strawberry"});
 
        // 为AutoCompleteTextView设置Adapter
        autoCompleteTextView.setAdapter(adapter);
 
        // 加载建议数据
        new LoadDataTask().execute();
    }
 
    // 异步任务,加载建议数据
    private class LoadDataTask extends AsyncTask> {
        @Override
        protected List doInBackground(Void... params) {
            // 从网络上或本地读取建议数据
            List suggestions = new ArrayList();
            // ...
            return suggestions;
        }
 
        @Override
        protected void onPostExecute(List suggestions) {
            // 创建一个Adapter
            ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,
                    android.R.layout.simple_dropdown_item_1line, suggestions

相关内容

热门资讯

分享实测!福建13水插件,We... 分享实测!福建13水插件,We poker辅助器下载,新版2025教程(有挂分享);是一款可以让一直...
必看攻略!蜀渝牌乐汇挂机软件,... 必看攻略!蜀渝牌乐汇挂机软件,wepoker代打辅助机器人,AI教程(有挂分享);蜀渝牌乐汇挂机软件...
最新技巧!海螺众娱辅助脚本,w... 最新技巧!海螺众娱辅助脚本,wepoker有机器人吗,德州教程(竟然有挂)是一款可以让一直输的玩家,...
来一盘!桂麻圈辅助,hhpok... 来一盘!桂麻圈辅助,hhpoker德州牛仔视频,AI教程(有挂存在);来一盘!桂麻圈辅助,hhpok...
9分钟了解!蜀渝牌乐汇挂机软件... 9分钟了解!蜀渝牌乐汇挂机软件,wepoker代打辅助机器人,我来教教你(有挂教学);是一款可以让一...
发现玩家!哈局八张有没有透视挂... 发现玩家!哈局八张有没有透视挂,hhpoker有后台操控吗,曝光教程(详细教程)是一款可以让一直输的...
揭秘关于!仙桃晃晃辅助器,we... 揭秘关于!仙桃晃晃辅助器,wepoker辅助器安装包,黑科技教程(有挂教程);揭秘关于!仙桃晃晃辅助...
最新研发!微信小程序功夫川辅助... 最新研发!微信小程序功夫川辅助,wepokerplus脚本,科技教程(有挂秘诀)是一款可以让一直输的...
技巧知识分享!来几局辅助器开挂... 技巧知识分享!来几局辅助器开挂,hhpoker辅助挂是真的吗,揭秘教程(有挂秘诀)是一款可以让一直输...
玩家必用!哥哥打大a辅助神器,... 玩家必用!哥哥打大a辅助神器,hhpoker真的有透视吗,透明挂教程(有挂工具)是一款可以让一直输的...