创建AutocompleteTextView,如gmail-> send letter-> tp

时间:2017-10-16 12:07:33

标签: android material-design

你能说出如何在gmail中创建AutocomplteTextView - > send letter->至。我尝试在我的应用程序中创建它,但它在7 android中非常糟糕。我的ui源代码:

<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创建了典型的自定义适配器,它实现了Filterable

1 个答案:

答案 0 :(得分:0)

你可以这样做。在您的xml文件中定义autocomplete edittext

<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" />

在你的活动中

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

}