📅  最后修改于: 2023-12-03 15:03:03.880000             🧑  作者: Mango
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.
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());
The MultiAutoComplete widget offers several customization options:
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.