如何将单选按钮放在 editText 上

How to put radio button on the editText

这是一个性别检查按钮。

<EditText
      android:id="@+id/txtSex"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginRight="29dp"
      android:layout_weight="1"
      android:ems="10"
      android:inputType="textPersonName"
      android:text=""/>

在我看来,您只需要在 RadioGroup 下方添加一条线。只需使用高度为 1dp 和背景颜色的视图。

要实现这种类型的 UI 需要水平放置两个单选按钮。取View后根据底线高度设置高度。并将其放在两个单选按钮的垂直方向。

试试这个方法

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Gender" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:orientation="vertical">

        <RadioGroup
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="Male" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:text="Female" />

        </RadioGroup>

        <View
            android:layout_width="250dp"
            android:layout_height="2dp"
            android:background="@android:color/black" />

    </LinearLayout>
</LinearLayout>

输出

如果只需要在RadioButton 下方加下划线,则无需将其放在edittext 中。只需将视图如下所示,

 <View
    android:layout_width="100dp"
    android:layout_height="1dp"
    android:layout_marginTop="10dp"
    android:background="#ccc" />