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

Android Studio布局开发:如何在EditText前添加固定前置标签

Android Kotlin项目EditText前置固定标签实现问题

我正在开发一个Kotlin项目,目前在查找屏幕布局相关的开发文档。我需要实现一个基础需求:在EditText控件前添加类似HTML中label的固定标签/提示文本,和如下HTML代码实现的效果一致:

<form>
  <label for="fname">First name:</label>
  <input type="text" id="fname" name="fname"><br><br>
</form>

最终渲染效果为:First name: ___________
目前我只找到android:hint属性,但它仅在EditText内容为空时才会显示,不符合我的需求。我有三个疑问:

  • 是否需要为每个标签单独添加PlainText类控件?
  • 要怎么将它和EditText绑定约束?
  • 有没有对应的分组组件可以实现该需求?

收到回答后的更新

看完大家的回答后我了解到需要自行组合控件实现,我目前没有使用TextInputLayout,最终的实现方案如下:
首先将显示“Date of Birth”的TextView与父布局绑定:

<TextView
        android:id="@+id/dobLabel2"
        android:layout_width="92dp"
        android:layout_height="23dp"
        android:layout_marginStart="76dp"
        android:layout_marginBottom="112dp"
        android:labelFor="@id/dobInputText"
        android:text="Date of Birth"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

再将EditText与上述TextView绑定:

<com.google.android.material.textfield.TextInputEditText
        android:id="@+id/dobInputText"
        android:layout_width="240dp"
        android:layout_height="54dp"
        android:hint="Day/Month/Year"
        app:layout_constraintStart_toStartOf="@+id/dobLabel2"
        app:layout_constraintTop_toBottomOf="@+id/dobLabel2" />

调整layout_constraint属性还可以将EditText放置在标签的左侧。感谢大家的建议,我已经可以解决该问题了。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 05:36:05