slider - ArrayAdapter OnItemSelectedListener 不起作用
问题描述
我创建了一个 arrayAdapter 来显示 MaterialDropdownMenu 中的项目,我希望 TextView 将文本设置为所选项目,但文本永远不会改变。为什么不这样做?谢谢
这里是 Kotlin 文件:
val options = arrayOf("Item 1", "Item 2", "Item 3")
val arrayAdapter = ArrayAdapter(this, R.layout.options_slider, options)
autoOptions.setText(arrayAdapter.getItem(0).toString(),false)
autoOptions.setAdapter(arrayAdapter)
autoOptions.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
tv1.text=options[position]
}
}
和xml文件:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/menu"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Settings">
<AutoCompleteTextView
android:id="@+id/autoOptions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
/>
<TextView
android:id="@+id/tv1"
android:layout_width="300dp"
android:layout_height="25dp"
android:text="test"
android:textAlignment="center">
</TextView>
</com.google.android.material.textfield.TextInputLayout>
这里是options_slider的xml文件:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:padding="16dp"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1">
</TextView>
解决方案
推荐阅读
- c++ - 为什么我的地图迭代器中有语法错误?
- java - 无法从 sharedpreference 中读取字符串集(变量值消失)
- python - 使用 beautifulsoup4 抓取时数据丢失
- windows - Jenkinsfile windows cmd 输出变量参考
- javascript - firebase.auth 不是一个函数 - 具有多个 firebase 应用程序初始化
- python - Tkinter - 如何根据输入框用户输入设置变量
- python - 如何根据输入字符串中 = 的出现从大字符串中提取子字符串,从而根据 = 符号的出现产生两个列表
- azure - 删除冗余高级页面 Blob - LRS 快照
- java - Jdbc模板的Java模棱两可的方法错误
- bash - 如果在 bashscript 中出现故障