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

相关内容

热门资讯

今日科普!微信卡五星辅助器,微... 今日科普!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)攻略;1.微信卡五星辅助器 选牌创建...
重大通报!微乐自建房辅助工具挂... 重大通报!微乐自建房辅助工具挂,微乐小程序免费黑科技(开挂自建房)app1、打开软件启动之后找到中间...
让我来分享经验!微乐自建房辅助... 让我来分享经验!微乐自建房辅助工具免费app,微乐自建房脚本免费入口(开挂自建房)脚本1、微乐自建房...
记者揭秘!微乐小程序自建房辅助... 记者揭秘!微乐小程序自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)软件微乐小程序自建房辅助...
必看攻略!微乐自建房辅助工具免... 必看攻略!微乐自建房辅助工具免费app,微乐家乡麻将自建房辅助app(开挂自建房)攻略1、每一步都需...
必备攻略!微乐家乡麻将自建房辅... 必备攻略!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)技巧1、必备攻略!...
查到实测辅助!微信卡五星辅助器... 查到实测辅助!微信卡五星辅助器,微乐家乡麻将自建房辅助app(开挂自建房)教程1、超多福利:超高返利...
传递经验!微乐自建房辅助工具挂... 传递经验!微乐自建房辅助工具挂,微乐小程序免费黑科技(开挂自建房)技巧1、操作简单,无需微乐自建房辅...
一分钟教你!微信卡五星辅助器,... 一分钟教你!微信卡五星辅助器,微乐家乡麻将自建房辅助app(开挂自建房)插件1、这是跨平台的微乐家乡...
传递经验!微乐小程序自建房辅助... 传递经验!微乐小程序自建房辅助工具插件,微乐自建房脚本免费入口(开挂自建房)脚本传递经验!微乐小程序...