2017-10-16 11 views
0

gmail -> send letter-> TO와 같이 AutocomplteTextView를 만드는 방법을 말할 수 있습니까? 나는 내 애플 리케이션에서 그것을 만들려고 노력하지만 7 안드로이드에서 매우 나쁜 작품. 내 UI 소스 코드 :gmail-> send letter-> tp와 같은 AutocompleteTextView 만들기

<android.support.design.widget.TextInputLayout 
    style="@style/ABM.TextInputLayout" 
    android:id="@+id/til_receiverName" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="8dp" 
    android:layout_width="match_parent" 
    android:visibility="visible"> 

    <EditText 
     style="@style/EditText.DarkIndigo" 
     android:dropDownAnchor="@id/til_receiverName" 
     android:dropDownHeight="@dimen/auto_complete_text_view_list_size" 
     android:hint="@string/receiver_name" 
     android:id="@+id/receiver_name" 
     android:imeOptions="flagNoExtractUi|actionDone" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:maxLength="160" 
     android:popupBackground="@android:color/white" 
     android:popupElevation="0dp" /> 
</android.support.design.widget.TextInputLayout> 

내가 BaseAdapter에서 전형적인 사용자 정의 어댑터를 작성하고 당신은 이런 일을 할 수

+0

제비 자동 완성 글고

<AutoCompleteTextView android:id="@+id/email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/prompt_email" android:inputType="textEmailAddress" android:maxLines="1" android:singleLine="true" /> 

을 정의 –

답변

0

필터링 구현합니다. XML 파일에서 자동 완성 ... https://android-arsenal.com/details/1/3102 대해 작업에서는 라이브러리의

public class Activity ext.........implements LoaderCallbacks<Cursor> { 
    private static final int REQUEST_READ_CONTACTS = 0; 

    private AutoCompleteTextView mEmailView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    .......  
    mEmailView = (AutoCompleteTextView) findViewById(R.id.email); 
    populateAutoComplete(); 

    } 

    private void populateAutoComplete() { 
    if (!mayRequestContacts()) { 
     return; 
    } 

    getLoaderManager().initLoader(0, null, this); 
    } 

    private boolean mayRequestContacts() { 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { 
     return true; 
    } 
    if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) { 
     return true; 
    } 
    if (shouldShowRequestPermissionRationale(READ_CONTACTS)) { 
     Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE) 
       .setAction(android.R.string.ok, new View.OnClickListener() { 
        @Override 
        @TargetApi(Build.VERSION_CODES.M) 
        public void onClick(View v) { 
         requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS); 
        } 
       }); 
    } else { 
     requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS); 
    } 
    return false; 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
             @NonNull int[] grantResults) { 
    if (requestCode == REQUEST_READ_CONTACTS) { 
     if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      populateAutoComplete(); 
     } 
    } 
} 

@Override 
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { 
    return new CursorLoader(this, 
      // Retrieve data rows for the device user's 'profile' contact. 
      Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI, 
        ContactsContract.Contacts.Data.CONTENT_DIRECTORY), ProfileQuery.PROJECTION, 

      // Select only email addresses. 
      ContactsContract.Contacts.Data.MIMETYPE + 
        " = ?", new String[]{ContactsContract.CommonDataKinds.Email 
      .CONTENT_ITEM_TYPE}, 

      // Show primary email addresses first. Note that there won't be 
      // a primary email address if the user hasn't specified one. 
      ContactsContract.Contacts.Data.IS_PRIMARY + " DESC"); 
} 

@Override 
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) { 
    List<String> emails = new ArrayList<>(); 
    cursor.moveToFirst(); 
    while (!cursor.isAfterLast()) { 
     emails.add(cursor.getString(ProfileQuery.ADDRESS)); 
     cursor.moveToNext(); 
    } 

    addEmailsToAutoComplete(emails); 
} 

@Override 
public void onLoaderReset(Loader<Cursor> cursorLoader) { 

} 

private void addEmailsToAutoComplete(List<String> emailAddressCollection) { 
    //Create adapter to tell the AutoCompleteTextView what to show in its dropdown list. 
    ArrayAdapter<String> adapter = 
      new ArrayAdapter<>(LoginActivity2.this, 
        android.R.layout.simple_dropdown_item_1line, emailAddressCollection); 

    mEmailView.setAdapter(adapter); 
} 


private interface ProfileQuery { 
    String[] PROJECTION = { 
      ContactsContract.CommonDataKinds.Email.ADDRESS, 
      ContactsContract.CommonDataKinds.Email.IS_PRIMARY, 
    }; 

    int ADDRESS = 0; 
    int IS_PRIMARY = 1; 
} 

}