2017-09-13 4 views

Répondre

0

Vous ne pouvez pas atteindre cet objectif par EditText, vous devez utiliser AutoCompleteTextView

<AutoCompleteTextView 
android:id="@+id/autoCompleteTextView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
> 

en vous Activité Fichier onCreate

AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); 

    String selectedEmail; 
    List<String> emails = new ArrayList<>(); 
    Pattern emailPattern = Patterns.EMAIL_ADDRESS; // API level 8+ 
    //get runtime permission 

    Account[] accounts = AccountManager.get(YourActivityName.this).getAccounts(); 

    int i=0; 
    for (Account account : accounts) { 
     if (emailPattern.matcher(account.name).matches()) { 
      String possibleEmail = account.name+"\n"; 
      emails.add(possibleEmail); 
     } 
    } 

    ArrayAdapter<String> adapter = new ArrayAdapter<String> 
    (this,android.R.layout.simple_list_item_1,emails); 
    autoComplete.setAdapter(adapter); 

    //default selected email 
    selectedEmail = emails.get(0); 
    autoComplete.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      // You can get the user selected email here  
      String selectedEmail = emails.get(position) 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

     } 
    }); 

dans votre manifeste, vous devez ajouter l'autorisation

<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

Si vous développez pour API> = 23 (Marshmallow), vous devez également obtenir des autorisations d'exécution. Suivre ce pour les autorisations d'exécution

https://developer.android.com/training/permissions/requesting.html

+0

Avez-vous essayé cette @ykb? –

+0

Non @Shriyansh Gautam. Pas encore. – ykb

+0

s'il montre une erreur, vous pouvez me dire –