在RadioButton旁边添加EditText

mha*_*per 10 android android-edittext

我有一个自定义DialogPreference子类,我希望有一个RadioGroup带有三个单选按钮.前两个是RadioButton带有标题的vanilla 组件,但是对于第三个我想EditText直接放在它的右侧,所以我可以在选择该按钮时输入自定义值.

我已经尝试将第三个按钮放入水平线LinearLayout,EditText然后第三个按钮不再参与父节点RadioGroup.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <RadioGroup
            android:id="@+id/lactate_radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

        <RadioButton
                android:id="@+id/lactate_default_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lactate_default_value"/>

        <RadioButton
                android:id="@+id/lactate_calibrated_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/lactate_calibrated_value" />
        <LinearLayout android:orientation="horizontal"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content">
            <RadioButton
                    android:id="@+id/lactate_custom_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/lactate_custom_value"/>

            <EditText
                    android:id="@+id/lactate_custom_value_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

        </LinearLayout>
    </RadioGroup>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

自定义DialogPreference的屏幕截图

我也试过以LinearLayout编程方式添加它,但仍然有相同的行为.有没有办法做到这一点,也许是通过明确告诉父母RadioGroup关于第三个按钮或通过某种方式EditText适当地定位,而不使用水平LinearLayout?否则,我想我必须编写一个RadioButton应该是真正的派对的子类!

cod*_*gic 5

您应该能够将父布局更改为RelativeLayout. 然后给你RadioGroup

android:orientation="vertical"
Run Code Online (Sandbox Code Playgroud)

然后将您的放在 . 的EditText旁边并与其底部对齐RadioGroup。我还没有尝试过,但这样的事情应该可行。

更新

我有时间来测试一下。这似乎提供了您正在寻找的输出

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<RadioGroup
        android:id="@+id/lactate_radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

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

    <RadioButton
            android:id="@+id/lactate_calibrated_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="lactate_calibrated_value" />
        <RadioButton
                android:id="@+id/lactate_custom_value"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="lactate_custom_value"/>
    </RadioGroup>

        <EditText
            android:id="@+id/lactate_custom_value_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/lactate_radio"
            android:layout_toRightOf="@+id/lactate_radio"
            android:text="Test Text" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

请注意,我还更改了一些widths 以wrap_content使其适合在一起。改变orientationRadioGroup垂直省去了需要LinearLayout

  • @codeMagic,“RelativeLayout”现在似乎已被弃用,自 2013 年以来是否出现了更直接的东西? (2认同)