在RadioButton旁添加EditText

11

我有一个自定义的DialogPreference子类,其中我想要有一个带有三个单选按钮的RadioGroup。前两个是带有标题的简单RadioButton组件,但对于第三个,我想将一个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>

自定义 DialogPreference 的屏幕截图

我也尝试通过程序动态添加LinearLayout,但仍然出现了相同的情况。是否有办法做到这一点,比如通过显式地告诉父RadioGroup有第三个按钮,或者以某种方式适当地定位EditText而不使用水平LinearLayout?否则,我认为我将不得不编写一个RadioButton子类,这应该是一场真正的派对!


那么使用RelativeLayout包含Linear,其中包含RadioGroup,然后根据其id属性将EditText放置在第三个RadioButton旁边,如何? - TronicZomB
由于“EditText”是完全不同的视图,因此它不能成为“RadioGroup”的一部分。您可以通过编程方式启用和禁用,而第三个单选按钮被点击时是实现类似功能的方法。希望您能理解。 - Vikalp Patel
2个回答

5

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

android:orientation="vertical"

接着将您的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>

请注意,我还将一些 width 改为 wrap_content 以便它们能够拟合在一起。将 RadioGrouporientation 更改为垂直方向可消除对 LinearLayout 的需要。

如果有一种像我原来的帖子那样的方法就太棒了。比如说,如果有人想要在每个RadioButton旁边放置一个EditText。不过,无论如何,这似乎是我的情况下可行的解决方案。在接受你的答案之前,我只是等待看看是否有其他人能提供更好的解决方案。谢谢! - mharper
1
是的,如果有内置的东西做这个事情会很好,但据我所知目前还没有。不过,你仍然可以轻松地使用alignToRightOf在每个中使用layout_below,并将每个EditText放置在前一个下方。不用谢,我理解你想等待看看是否有更好的方法。 - codeMagic
2
@codeMagic,RelativeLayout现在似乎已经被弃用了,自2013年以来是否出现了更直接的替代方案? - Chau

0
<LinearLayout
  android:layout_height= "wrap_content"
  android:layout_width= "wrap_content"
  android:orientation= "horizontal"
>

 <LinearLayout
   android:layout_height= "wrap_content"
   android:layout_width= "wrap_content"
   android:orientation= "vertical"
 >

<RadioGroup
  <!-- radio buttons code here -->
/>

 </LinearLayout>

 <LinearLayout
   android:layout_height= "wrap_content"
   android:layout_width= "wrap_content"
 >

 <EditText

 <!-- edit text code here -->
     android:layout_alignBottom="@+id/id_ofRadioButton"
     android:layout_toRightOf="@+id/id_ofRadioButton"
 />

 </LinearLayout>

如果您想在每个单选按钮旁边编辑文本:

<LinearLayout
  android:layout_height= "wrap_content"
  android:layout_width= "wrap_content"
  android:orientation= "vertical"
>

 <LinearLayout
   android:layout_height= "wrap_content"
   android:layout_width= "wrap_content"
   android:orientation= "horizontal"
 >

<RadioGroup
  <!-- single radio button code here -->
/>

 <EditText

 <!-- edit text code here -->

 />

 </LinearLayout>


<LinearLayout
  android:layout_height= "wrap_content"
  android:layout_width= "wrap_content"
  android:orientation= "vertical"
>

 <LinearLayout
   android:layout_height= "wrap_content"
   android:layout_width= "wrap_content"
   android:orientation= "horizontal"
 >

<RadioGroup
  <!-- single radio button code here -->
/>

 <EditText

 <!-- edit text code here -->

 />

 </LinearLayout>


类似android:layout_toRightOf这样的属性在LinearLayout中不可用。此外,在您的第二个解决方案中,RadioButton将被从RadioGroup中移除。 - codeMagic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接