Kotlin中的文本字段下拉菜单不起作用

qyuhtwio  于 2022-12-13  发布在  Kotlin
关注(0)|答案(1)|浏览(135)

下面是我的XML代码

<?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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        >
    </com.google.android.material.textfield.TextInputLayout>
    <AutoCompleteTextView
        android:id="@+id/auto_complete_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
        android:hint="@string/select"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

这是我的.kt文件

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     super.onViewCreated(view, savedInstanceState)
     val autocompleteTextView = view.findViewById<AutoCompleteTextView>(R.id.auto_complete_txt)
//     System.out.println(autocompleteTextView)
     val items = arrayOf("Test","Test1","Test2","Test3","Test4","Test5","Test6")
        val adapter  = ArrayAdapter(requireContext(),android.R.layout.simple_list_item_1,items)
        autocompleteTextView.setAdapter(adapter)
//                autocompleteTextView.setOnItemClickListener(AdapterView.OnItemClickListener())
    }

我试着做下面这个例子https://www.youtube.com/watch?v=EBhmRaa8nhE
如对上述问题有任何建议,我们将不胜感激。

yc0p9oo0

yc0p9oo01#

因为您的AutoCompleteTextView不在TextInputLayout内。您必须这样撰写。

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp">

 <AutoCompleteTextView
    android:id="@+id/auto_complete_txt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="none"
    android:hint="@string/select"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
    />
</com.google.android.material.textfield.TextInputLayout>

相关问题