You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Kotlin中点击Switch控件实现文本框展开/收起的方法

Android Kotlin实现Switch控件控制文本框展开/收起

步骤1:编写布局文件

在布局XML中关联Switch与需要展开的文本框,默认隐藏文本框:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <!-- 控制开关 -->
    <Switch
        android:id="@+id/switch_expand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="展开更多内容"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>

    <!-- 可展开的文本框,默认隐藏 -->
    <EditText
        android:id="@+id/et_expandable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入内容"
        android:visibility="gone"
        app:layout_constraintTop_toBottomOf="@id/switch_expand"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="8dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

步骤2:Kotlin逻辑实现

在Activity/Fragment中给Switch设置状态监听,根据选中状态切换文本框显示/隐藏:

import android.os.Bundle
import android.widget.EditText
import android.widget.Switch
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val expandSwitch = findViewById<Switch>(R.id.switch_expand)
        val expandableEt = findViewById<EditText>(R.id.et_expandable)

        // 开关状态监听
        expandSwitch.setOnCheckedChangeListener { _, isChecked ->
            // 根据选中状态切换文本框可见性
            expandableEt.visibility = if (isChecked) {
                android.view.View.VISIBLE
            } else {
                android.view.View.GONE
            }
        }
    }
}

多组控件的处理(解决之前Button触发全部的问题)

如果页面有多组Switch和文本框,只需给每组控件设置唯一ID,分别绑定监听即可:

// 第一组
val switch1 = findViewById<Switch>(R.id.switch_expand_1)
val et1 = findViewById<EditText>(R.id.et_expandable_1)
switch1.setOnCheckedChangeListener { _, isChecked ->
    et1.visibility = if (isChecked) android.view.View.VISIBLE else android.view.View.GONE
}

// 第二组
val switch2 = findViewById<Switch>(R.id.switch_expand_2)
val et2 = findViewById<EditText>(R.id.et_expandable_2)
switch2.setOnCheckedChangeListener { _, isChecked ->
    et2.visibility = if (isChecked) android.view.View.VISIBLE else android.view.View.GONE
}

可选:添加过渡动画

如果需要更流畅的展开/收起效果,可添加动画过渡:

expandSwitch.setOnCheckedChangeListener { _, isChecked ->
    if (isChecked) {
        expandableEt.visibility = android.view.View.VISIBLE
        expandableEt.animate().alpha(1f).duration = 300
    } else {
        expandableEt.animate().alpha(0f).setDuration(300).withEndAction {
            expandableEt.visibility = android.view.View.GONE
        }
    }
}

内容的提问来源于stack exchange,提问作者the tiffie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 21:23:10