android how an EditText work as AutoComplete

前端 未结 5 1601
孤独总比滥情好
孤独总比滥情好 2020-12-03 02:38

I want my EditText should work as AutoComplete, for that I write in XML file

android:inputType=\"textAutoComplete|textAutoCorrect\"         


        
5条回答
  •  情深已故
    2020-12-03 03:03

    I use this code:

    1) On AndroidManifest.xml

    
    

    2) On xml layout you must use AutoCompleteTextView instead of EditText.

    
    

    3) Use this on Activity file

    private ArrayAdapter getEmailAddressAdapter(Context context) {
        Account[] accounts = AccountManager.get(context).getAccounts();
        String[] addresses = new String[accounts.length];
        for (int i = 0; i < accounts.length; i++) { 
            addresses[i] = accounts[i].name;
        }
        return new ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, addresses);
    }
    

    4) On onCreate activity:

    AutoCompleteTextView autoCompleteTextView1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
    autoCompleteTextView1.setAdapter(getEmailAddressAdapter(this));
    

提交回复
热议问题