简体   繁体   English

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

[英]How can i auto-fill an edit text from it's mid?

I am working on a contact manager project for android.In which i want to auto-fill the email address ending part in the field at the time of signup by the user. 我正在为android.In开发一个联系人管理器项目,我希望在用户注册时自动填充该字段中的电子邮件地址结尾部分。 For example when user type his or her username it should be automatically gives the suggestion for @gmail.com or @outlook.com and so on. 例如,当用户输入他或她的用户名时,它应该自动给出@ gmail.com或@ outlook.com的建议,依此类推。

Well,This is a little part of my code 嗯,这是我的代码的一小部分

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);

Well, I got output like this 好吧,我有这样的输出

图像在这里

But I want that when user enter his/her username then this suggestion should be appear but it din't. 但是我希望当用户输入他/她的用户名时,应该出现这个建议,但它不是。

图像在这里

Well, My question is not duplicate of android how an EditText work as AutoComplete this question.My problem is differ from this. 好吧,我的问题不是android的重复, 如何将EditText作为自动完成这个问题。我的问题与此不同。

Thanks in advance. 提前致谢。

I suggest that you append the text from email to each string in your maindb before setting the adapter --> use TextWatcher to detect changes on mail view . 我建议您在设置适配器之前将电子邮件中的文本附加到maindb中的每个字符串 - >使用TextWatcher检测邮件视图上的更改。

Edit 编辑

may be this what you are looking for 也许这就是你要找的东西

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);
                }
        }
    });

Hope this will help 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM