📜  MultiAutoComplete -Android (1)

📅  最后修改于: 2023-12-03 15:03:03.880000             🧑  作者: Mango

MultiAutoComplete - Android

MultiAutoComplete is an Android widget that provides a text field that allows users to enter multiple values separated by a delimiter, and offers suggestions for possible values based on a database query. This widget is useful for searches, tagging, and any other use case where the user needs to enter multiple values.

Features
  • Allows users to enter multiple values separated by a delimiter.
  • Displays possible values as suggestions based on a database query.
  • Allows the use of a custom adapter to display the suggestions.
  • Offers a customizable threshold for the number of characters that must be typed before suggestions are displayed.
Getting Started

To use MultiAutoComplete in your Android app, you need to add the following dependency to your build.gradle file:

dependencies {
    implementation 'com.mcxiaoke.view:library:2.1.0'
}

You can now add a MultiAutoComplete widget to your layout XML file:

<com.mcxiaoke.view.MultiAutoCompleteTextView
    android:id="@+id/multiAutoCompleteTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text"
    android:imeOptions="actionDone"
    android:inputType="text"
    android:completionThreshold="3"
    app:delimiter=","
    app:popupBackground="@android:color/white"
    app:suggestionsMaxHeight="200dp"
    app:suggestionsTextColor="@android:color/black" />

In your Java code, you need to set up a custom adapter and a database query to populate the suggestions list:

MultiAutoCompleteTextView multiAutoCompleteTextView = findViewById(R.id.multiAutoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, items);
multiAutoCompleteTextView.setAdapter(adapter);
multiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
Customization

The MultiAutoComplete widget offers several customization options:

  • app:delimiter: sets the delimiter that separates the multiple values entered by the user. Defaults to a comma (,).
  • app:popupBackground: sets the background color of the suggestions popup. Defaults to white.
  • app:suggestionsMaxHeight: sets the maximum height of the suggestions popup. Defaults to 200dp.
  • app:suggestionsTextColor: sets the text color of the suggestions popup. Defaults to black.
  • android:completionThreshold: sets the minimum number of characters that must be entered before suggestions are displayed. Defaults to 1.
Conclusion

MultiAutoComplete is a powerful and flexible widget that allows users to enter multiple values with ease, while providing useful suggestions based on a database query. With its customizable options and easy implementation, it is a great addition to any Android app that requires text input.