android - 是否可以使用微调器实现公开的下拉菜单

标签 android material-design android-view autocompletetextview

Material design 有一个使用 AutoCompleteTextView 实现的公开下拉菜单,如下所示:

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text">

  <AutoCompleteTextView
      android:id="@+id/filled_exposed_dropdown"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

但我需要具有相同的外观和感觉,但具有微调器行为(没有自动完成,只需显示弹出窗口并选择一个项目)。

我禁用了对 AutoCompleteTextView 的编辑以避免使用自动完成功能,但在选择其中一项后,自动完成功能仅列出与该 View 中使用的筛选器所选择的项目文本相匹配的项目。这是代码:

    <AutoCompleteTextView
            android:id="@+id/filled_exposed_dropdown"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="16dp"
            **android:editable="false"**/>

我还设置了一个监听器,在点击它时打开项目列表

val editTextFilledExposedDropdown = findViewById<AutoCompleteTextView>(R.id.filled_exposed_dropdown)
    editTextFilledExposedDropdown.setAdapter(adapter)
    editTextFilledExposedDropdown.setOnClickListener {
        (it as AutoCompleteTextView).showDropDown()
}

所以,我想知道是否可以使用微调器实现它

enter image description here

这是我使用微调器的尝试,但它没有正确显示样式 OutlinedBox.Dense.ExposedDropdownMenu 并且还显示两个箭头底部图标,我认为一个用于样式,另一个用于微调器。

这是微调器的代码:

<com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hint">
    <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</com.google.android.material.textfield.TextInputLayout>


<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/layout_id"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Currency">
    <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/my_spinner"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</com.google.android.material.textfield.TextInputLayout>

最佳答案

这就是我一直在寻找的,只需通过覆盖 getFilter 方法来禁用 AutoCompleteTextView 的自动完成功能。 答案张贴在这里 ( AutoCompleteTextView - disable filtering )

关于android - 是否可以使用微调器实现公开的下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56633948/

相关文章:

android - 为什么我不能在 Activity 中使用 FloatingActionButton

android - 什么是 LayoutInflater 以及如何正确使用它?

java - 如何判断View状态是否正在恢复

android - 我如何从安卓手机接收网站的值(value)

android - 是否可以使用没有阴影的 OverlayItem.setMarker()?

android - 将 RxJava Observable 绑定(bind)到 TextView 的文本属性

android - 如何以编程方式在 CoordinatorLayout 中显示折叠的元素?

android - 为什么 MaterialButton 文本不包含完整空间?

特定分辨率的 Android Drawable 文件夹

android - Android 中的 onTouchListener 将检测所有父 View 上的触摸