java - 如何根据 Spinner 的选择将纯文本视图添加到当前布局?
问题描述
我是 Kotlin、Java 和 Android Studio 的新手,但需要为暑期实习开发一个类似调查的 Android 应用程序。在 SO 社区的帮助下,我已经克服了一些看似艰难的障碍(我是这里唯一的“开发人员”),但这让我感到困惑,我在 SO 上没有找到任何关于它的信息。
我有一个自定义微调器,其中包含不同类型屋顶的值(在此处找到 NoDefaultSpinner 类),并且在选择“其他”时,如果可能的话,我希望纯文本视图在同一布局上可见。有什么办法可以做到这一点,还是我必须通过添加另一个活动屏幕来处理“其他”的情况?
这是我到目前为止所拥有的:
class PollSectionInfoActivity : AppCompatActivity() {
private var otherSelected = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_poll_section_info)
val spinner = findViewById<Spinner>(R.id.roofType)
var roofTypes : Array<String> = emptyArray()
roofTypes += "BUR"
roofTypes += "Liquid Applied"
roofTypes += "Modified Bitumen"
roofTypes += "Protective Membrane Assembly"
roofTypes += "Single Ply"
roofTypes += "Shingles"
roofTypes += "Tile"
roofTypes += "Other"
roofTypes += "Unknown"
val nextButton = findViewById<Button>(R.id.next_button)
nextButton.isEnabled = false
val adapter : ArrayAdapter<String> = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, roofTypes)
spinner.adapter = adapter
spinner.onItemSelectedListener
spinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val selectedItem = parent!!.getItemAtPosition(position).toString()
this@PollSectionInfoActivity.otherSelected = selectedItem == "Other"
nextButton.isEnabled = true
}
}
}
//TODO: update code to pass selection to next layout.
fun nextButton() {
val spinner = findViewById<Spinner>(R.id.roofType)
val selection = spinner.selectedItem.toString()
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
tools:context=".PollSectionInfoActivity">
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:text="@string/button_next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<com.testapp.NoDefaultSpinner
android:id="@+id/roofType"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:prompt="@string/select_roof_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:checked="false"
android:text="@string/surfacing"
android:visibility="visible"
app:layout_constraintStart_toStartOf="@+id/roofType"
app:layout_constraintTop_toBottomOf="@+id/roofType" />
此外,我们非常欢迎您提供任何额外的帮助或建议!我在大学里只上过一门(相当枯燥的)软件课程,所以我对这一切都不太了解。另外,无论如何,我更像是一个理论家。提前致谢!
PS如果在其他地方问过这个问题,我深表歉意;老实说,我找不到解决方案,或者至少找不到解决方案。
解决方案
您可以将 TextView 添加到布局中并将可见性设置为不可见。
<TextView
android:id = "@+id/textView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:visibility = "invisible"/>
在 OnItemSelected 方法中,如果用户选择“其他”,您可以像这样将 TextView 可见性设置为可见
textView.visibility = View.VISIBLE
推荐阅读
- ionic-framework - 离子框架 Braintree 集成
- javascript - 为什么'load'的EventListener在身体底部工作,而不是在顶部?
- javascript - 使用自定义事件呈现模板未正确显示 Fullcalendar 的事件
- reactjs - 使用反应路由器切换到结果组件“onSubmit”在反应钩子中的搜索表单
- tortoisesvn - 在 TortoiseSVN 中添加未版本控制的文件作为新版本
- python - 我正在尝试使用列表在 python IDLE 中选择一个字符。但是,它要么循环输入,要么冻结窗口,所以我必须重新开始
- javascript - 将数据从后端(nodejs)传递到前端javascript
- powerbi - PowerBI - 找不到新的计算表
- python - Altair:将 Log10 轴裁剪到所需的间隔(域)
- asp.net-mvc - 如何在 ASP.NET MVC 中执行混合个人用户帐户/aad 身份验证?