android comment un EditText fonctionne comme AutoComplete

Mots clés : androidandroid-edittextandroid

meilleur 4 Réponses android comment un EditText fonctionne comme AutoComplete

vote vote

93

String[] countries = getResources().getStringArray(R.array.list_of_countries); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countries); actv.setAdapter(adapter); 
vote vote

89

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission> 
<AutoCompleteTextView     android:id="@+id/autoCompleteTextView1"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:ems="10"     android:text="AutoCompleteTextView" /> 
private ArrayAdapter<String> 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<String>(context, android.R.layout.simple_dropdown_item_1line, addresses); } 
AutoCompleteTextView autoCompleteTextView1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); autoCompleteTextView1.setAdapter(getEmailAddressAdapter(this)); 
vote vote

73

public class AutoSuggestAdapter extends ArrayAdapter {     @Override     public View getView(int position, View convertView, ViewGroup parent)     {         // handle view here     }      @Override     public Filter getFilter()     {        // implement filtering here     } } 
vote vote

65

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,codeKeyWords); MultiAutoCompleteTextView autoCompleteTextView1 = (MultiAutoCompleteTextView) findViewById(R.id.autoCompleteTextView1); autoCompleteTextView1.setAdapter(adapter); autoCompleteTextView1.setThreshold(1); autoCompleteTextView1.setTokenizer(new this.CommaTokenizer()); 
/**          * This simple Tokenizer can be used for lists where the items are          * separated by a comma and one or more spaces.          */     public static class CommaTokenizer implements Tokenizer {         public int findTokenStart(CharSequence text, int cursor) {             int i = cursor;              while (i > 0 && text.charAt(i - 1) != ' ') {                 i--;             }             while (i < cursor && text.charAt(i) == '\n') {                 i++;             }              return i;         }          public int findTokenEnd(CharSequence text, int cursor) {             int i = cursor;             int len = text.length();              while (i < len) {                 if (text.charAt(i) == '\n') {                     return i;                 } else {                     i++;                 }             }              return len;         }          public CharSequence terminateToken(CharSequence text) {             int i = text.length();              while (i > 0 && text.charAt(i - 1) == ' ') {                 i--;             }              if (i > 0 && text.charAt(i - 1) == ' ') {                 return text;             } else {                 if (text instanceof Spanned) {                     SpannableString sp = new SpannableString(text + "\n");                     TextUtils.copySpansFrom((Spanned) text, 0, text.length(),                                             Object.class, sp, 0);                     return sp;                 } else {                     return text + " ";                 }             }         } 

Questions similaires