java - 如何根据微调器的选择将纯 TextView 添加到当前布局?

标签 java android android-layout android-studio kotlin

我是 Kotlin、Java 和 Android Studio 的新手,但需要为暑期实习开发一个类似调查的 Android 应用程序。在 SO 社区(我是这里唯一的“开发者”)的帮助下,我已经克服了一些看似困难的障碍,但是这个问题让我感到困惑,我还没有在 SO 上找到任何关于它的信息。

我有一个自定义微调器,其中包含不同类型屋顶的值(NoDefaultSpinner 类找到 here ),并且在选择“其他”时,如果可能的话,我希望纯 TextView 在同一布局上变得可见。有什么办法可以实现这种情况,还是我必须通过添加另一个 Activity 屏幕来处理“其他”的情况?

这是我到目前为止所拥有的:

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" />

此外,我们非常欢迎您提供任何其他帮助或建议!我在大学只学过一门(相当沉闷的)软件类(class),所以我对这一切不太了解。另外,无论如何,我更像是一个理论家。提前致谢!

附注如果在其他地方有人问过这个问题,我深表歉意;老实说,我找不到解决方案,或者至少找不到解决方案。

最佳答案

您可以将 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

关于java - 如何根据微调器的选择将纯 TextView 添加到当前布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50804705/

相关文章:

Java:获得一次锁更好还是多次获得锁更好?

java - 如何在 Jsp 或 Servlet 中更改请求 URl

android - 显示 Realm 数据的通用适配器

java - 标签栏粘在顶部而不是折叠工具栏下方

java - 如何在Android中滚动项目时添加圆角作为Recyclerview的背景

java - 编译器检查类的注释

java - XML 图片更改

android - 带页眉和页脚的 ListView

java - Eclipse 在一段时间后停止突出显示引用

android - RecyclerView scrollToPosition 不触发 scrollListener