我有以下 AppCompatAutoCompleteTextView -
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/phoneNumberPrefixTextView"
style="@style/PrimaryEditText"
android:layout_width="match_parent"
android:layout_height="@dimen/credit_card_form_height"
android:gravity="center"
android:dropDownWidth="match_parent"
android:inputType="number"
android:maxLength="3" />
使用以下列表项 XML -
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="@dimen/padding_16">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/country_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/padding_16"
android:layout_marginVertical="@dimen/padding_16"
android:fontFamily="@font/montserrat_regular"
android:textColor="@color/light_black"
android:textSize="@dimen/text_size_normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Israel" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/country_code_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/padding_4"
android:fontFamily="@font/montserrat_regular"
android:textColor="@color/fog_medium"
android:textSize="@dimen/text_size_normal"
app:layout_constraintBottom_toBottomOf="@+id/country_edit_text"
app:layout_constraintStart_toEndOf="@+id/country_edit_text"
app:layout_constraintTop_toTopOf="@+id/country_edit_text"
tools:text="+972" />
</androidx.constraintlayout.widget.ConstraintLayout>
以及以下 ArrayAdapter -
class CountriesWithCodeAdapter(
context: Context,
resource: Int,
private val items: MutableList<Countries.Country>
) : ArrayAdapter<Countries.Country>(context, resource, items) {
private val originalList: List<Countries.Country> = items.toList()
private var filteredItems: List<Countries.Country> = items.toList()
override fun getItem(position: Int): Countries.Country {
return items[position]
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val binding: CreditCardCountryWithCodeItemBinding =
if (convertView != null) {
CreditCardCountryWithCodeItemBinding.bind(convertView)
} else {
CreditCardCountryWithCodeItemBinding.inflate(
LayoutInflater.from(context),
parent, false
)
}
binding.countryEditText.text = items[position].name
binding.countryCodeEditText.text = items[position].callingCode
return binding.root
}
override fun getFilter(): Filter = object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val charString = constraint.toString().lowercase(Locale.getDefault()).trim()
filteredItems = if (charString.isEmpty()) {
originalList.toList()
} else {
originalList.filter { country ->
country.callingCode.contains(charString, ignoreCase = true)
}
}
val filterResults = FilterResults()
filterResults.values = filteredItems
return filterResults
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
@Suppress("UNCHECKED_CAST")
filteredItems = results?.values as? List<Countries.Country> ?: emptyList()
if (filteredItems.isEmpty()) {
clear()
notifyDataSetChanged()
} else {
clear()
addAll(filteredItems)
notifyDataSetChanged()
}
}
}
}
我正在尝试实现一种在下拉项中引入填充的行为。我在设置
android:dropDownWidth="match_parent"
的同时添加了列表项填充到根父级,但它似乎实际上没有做任何事情。我错过了什么?
假设您正在尝试在自动完成文本视图上实现水平边距,您可以尝试为自动完成文本视图创建单独的布局,并将其包含在父布局中,如下所示,这将在自动完成文本视图上设置 16 的水平填充/边距: -
父级布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="16dp"
tools:context=".messageCenter.MessageCenterFragment">
<include
android:id="@+id/ilAutoCompleteTextView"
layout="@layout/autocomplete_textview"/>
</androidx.constraintlayout.widget.ConstraintLayout>
子布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:hint="Enter Your Name Here"
android:padding="15dp"
android:textColorHint="#fff"
android:textStyle="bold|italic" />
</RelativeLayout>
现在是你的代码:-
val fruits = arrayOf("Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Mango", "Pear")
val adapter = ArrayAdapter(requireContext(), R.layout.select_dialog_item, fruits)
//Getting the instance of AutoCompleteTextView
val actv = binding.ilAutoCompleteTextView.autoCompleteTextView
actv.threshold = 1 //will start working from first character
actv.setAdapter(adapter) //setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.RED)