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

Android原生数据请求表单:仅当Insurance选Yes时显示Insurance Number字段

Android 表单动态显示字段实现方案

1. 编写XML布局文件

先在布局中定义Insurance?单选组,以及默认隐藏的Insurance Number输入区域:

<!-- 保险选择单选组 -->
<RadioGroup
    android:id="@+id/rg_insurance"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/rb_yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Yes" />

    <RadioButton
        android:id="@+id/rb_no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No"
        android:checked="true" /> <!-- 默认选中No -->
</RadioGroup>

<!-- 保险号输入区域,默认隐藏 -->
<LinearLayout
    android:id="@+id/ll_insurance_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone"
    android:layout_marginTop="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insurance Number" />

    <EditText
        android:id="@+id/et_insurance_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your insurance number" />
</LinearLayout>

2. 在代码中添加单选组监听逻辑

根据你使用的语言选择对应实现:

Kotlin 实现

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

        val rgInsurance = findViewById<RadioGroup>(R.id.rg_insurance)
        val llInsuranceNumber = findViewById<LinearLayout>(R.id.ll_insurance_number)
        val etInsuranceNumber = findViewById<EditText>(R.id.et_insurance_number)

        rgInsurance.setOnCheckedChangeListener { _, checkedId ->
            when(checkedId) {
                R.id.rb_yes -> {
                    // 选中Yes时显示保险号输入区域
                    llInsuranceNumber.visibility = View.VISIBLE
                    etInsuranceNumber.error = null
                }
                R.id.rb_no -> {
                    // 选中No时隐藏输入区域并清空内容
                    llInsuranceNumber.visibility = View.GONE
                    etInsuranceNumber.text?.clear()
                }
            }
        }

        // 表单提交按钮逻辑示例
        findViewById<Button>(R.id.btn_submit).setOnClickListener {
            val isInsuranceYes = rgInsurance.checkedRadioButtonId == R.id.rb_yes
            if (isInsuranceYes) {
                val insuranceNumber = etInsuranceNumber.text.toString().trim()
                if (insuranceNumber.isEmpty()) {
                    etInsuranceNumber.error = "Please enter insurance number"
                    return@setOnClickListener
                }
            }
            // 执行表单提交逻辑
        }
    }
}

Java 实现

public class YourFormActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_form_layout);

        RadioGroup rgInsurance = findViewById(R.id.rg_insurance);
        LinearLayout llInsuranceNumber = findViewById(R.id.ll_insurance_number);
        EditText etInsuranceNumber = findViewById(R.id.et_insurance_number);

        rgInsurance.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.rb_yes) {
                // 选中Yes时显示保险号输入区域
                llInsuranceNumber.setVisibility(View.VISIBLE);
                etInsuranceNumber.setError(null);
            } else if (checkedId == R.id.rb_no) {
                // 选中No时隐藏输入区域并清空内容
                llInsuranceNumber.setVisibility(View.GONE);
                etInsuranceNumber.setText("");
            }
        });

        // 表单提交按钮逻辑示例
        findViewById(R.id.btn_submit).setOnClickListener(v -> {
            boolean isInsuranceYes = rgInsurance.getCheckedRadioButtonId() == R.id.rb_yes;
            if (isInsuranceYes) {
                String insuranceNumber = etInsuranceNumber.getText().toString().trim();
                if (insuranceNumber.isEmpty()) {
                    etInsuranceNumber.setError("Please enter insurance number");
                    return;
                }
            }
            // 执行表单提交逻辑
        });
    }
}

3. 关键说明

  • 初始状态下将ll_insurance_number设为android:visibility="gone",确保默认不显示
  • 通过RadioGroup.setOnCheckedChangeListener监听选项变化,动态切换输入区域的显示状态
  • 表单提交时必须校验:如果选中Yes,则Insurance Number输入框不能为空,避免无效提交

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 02:43:16