Android 在单选按钮旁边添加编辑文本
我有一个自定义的Android 在单选按钮旁边添加编辑文本,android,android-edittext,Android,Android Edittext,我有一个自定义的DialogPreference子类,其中我希望有一个带有三个单选按钮的RadioGroup。前两个是带有标题的普通RadioButton组件,但对于第三个组件,我想在其右侧直接放置一个EditText,以便在选择该按钮时输入自定义值 我已尝试将第三个按钮与编辑文本一起放入水平线性布局,但第三个按钮不再参与父射线组 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
DialogPreference
子类,其中我希望有一个带有三个单选按钮的RadioGroup
。前两个是带有标题的普通RadioButton
组件,但对于第三个组件,我想在其右侧直接放置一个EditText
,以便在选择该按钮时输入自定义值
我已尝试将第三个按钮与编辑文本一起放入水平线性布局
,但第三个按钮不再参与父射线组
<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>
我还尝试以编程方式添加此
LinearLayout
,但仍然有相同的行为。有没有办法做到这一点,可以明确地告诉家长RadioGroup
关于第三个按钮,或者在不使用水平线性布局的情况下,以某种方式适当地定位EditText
?否则,我想我必须编写一个RadioButton
子类,它应该是一个真正的party 您应该能够将父布局更改为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
s更改为wrap\u content
,以便将其组合在一起。将放射组的方向
更改为垂直,无需使用线性布局
<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>
android:layout_alignBottom=“@+id/id_无线电按钮”
android:layout_toRightOf=“@+id/id_无线电按钮”
/>
如果要沿每个单选按钮的两侧编辑文本:
<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>
/>
/>
如果一个RelativeLayout然后Linear包含放射组,然后根据其id属性将EditText放置在第三个RadioButton旁边。因为EditText
是完全不同的视图,所以它不能是RadioGroup
的一部分。您可以通过编程方式启用
和禁用
,而单击第三个单选按钮是实现类似功能的方法。希望你能理解。如果有办法像我原来的帖子那样做到这一点,那就太好了。例如,想象一下,如果有人想在每个单选按钮旁边添加一个EditText
。无论如何,这似乎是一个可行的解决方案,我的情况。在接受你的答案之前,只是等着看其他人是否能拿出奇迹般的解决方案。谢谢是的,如果已经有内置的东西就好了,但恐怕没有。无论如何,您仍然可以通过在每个中使用alignitorightof
,然后在下面使用layout\u
并将每个EditText
放置在前一个下方来轻松完成。不客气,我理解等着看你有没有收获better@codeMagic,RelativeLayout
现在似乎已被弃用,2013年以来有没有更直接的东西出现?像android:layout\u toRightOf
这样的属性在LinearLayout
中不可用。另外,在第二种解决方案中,单选按钮
s将从RadioGroup