java - 我如何从中间自动填充编辑文本?

标签 java android autocomplete android-edittext android-arrayadapter

我正在为 android 开发一个联系人管理器项目。在该项目中,我想在用户注册时自动填写该字段中的电子邮件地址结尾部分。 例如,当用户输入他或她的用户名时,它应该自动给出 @gmail.com 或 @outlook.com 等的建议。

嗯,这是我的代码的一小部分

String[] maindb = {"@gmail.com", "@rediffmail.com", "@hotmail.com", "@outlook.com"};

mail = (AutoCompleteTextView) findViewById(R.id.A1_edt_mail);

ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, maindb);
mail.setAdapter(adpt);

好吧,我得到了这样的输出

Image is here

但我希望当用户输入他/她的用户名时,应该会出现这个建议,但它并没有出现。

Image is here

嗯,我的问题不是 android how an EditText work as AutoComplete 的重复问题 这个问题。我的问题与此不同。

提前致谢。

最佳答案

我建议您在设置适配器之前将电子邮件中的文本附加到 maindb 中的每个字符串 --> 使用 TextWatcher 来检测邮件 View 的更改。

编辑

这可能是你要找的东西

final ArrayList<String> maindb = new ArrayList<>();
    maindb.add("@gmail.com");
    maindb.add("@rediffmail.com");
    maindb.add("@hotmail.com");
    maindb.add("@outlook.com");
    final ArrayList<String> compare = new ArrayList<>();
    final ArrayAdapter<String> adpt = new ArrayAdapter<String>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, compare);
    Word.setAdapter(adpt);
    Word.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) {
            if (s.length() >= 0)
                if (!s.toString().contains("@")) {
                    compare.clear();
                    for (String aMaindb : maindb) {
                        aMaindb = s + aMaindb;
                        compare.add(aMaindb);
                    }
                    adpt.clear();
                    adpt.addAll(compare);
                }
        }
    });

希望对你有帮助

关于java - 我如何从中间自动填充编辑文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48994264/

相关文章:

java - JDeveloper POJO Web 服务 FileNotFoundException

java - 如何在方法体内进行注入(inject)?

javascript - jQuery UI 自动完成 - 返回页面下方的选择。不确定如何删除它

ajax - 如何将自定义值传递给 AutocompleteExtender?

android - 如何在应用关闭时发送通知?

php - Jquery UI 自动完成

java - 应用程序崩溃 : class has no empty constructor

java - 如何在 Antlr 中使用访问者创建自定义 AST

javascript - 原生 Android Facebook 登录的按下按钮效果

如果在处理时单击,Android 模拟器会崩溃