问题描述
我有一个自定义的对话preference
子类中,我想有一个 RadioGroup中
三单选按钮。前两个是香草单选
带标题的组件,但我想放在第三的的EditText
直接到正确的就那么被选中的按钮时,我可以输入自定义值。
I have a custom DialogPreference
subclass in which I would like to have a RadioGroup
with three radio buttons. The first two are vanilla RadioButton
components with titles, but for the third I would like to place an EditText
directly to the right of it so I can enter a custom value when that button is selected.
我试图把第三个按钮为水平的LinearLayout
沿的EditText
但随后的第三个按钮的功能不参加父 RadioGroup中
了。
I have tried putting the third button into a horizontal LinearLayout
along with the EditText
but then the third button does not participate in the parent RadioGroup
anymore.
<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 适当地不使用水平
的LinearLayout
?否则,我想我会写一个单选
子类,应该是一个真正的政党!
I have also tried adding this
LinearLayout
programmatically but still had the same behavior. Is there any way to do this, perhaps by explicitly telling the parent RadioGroup
about the third button or by somehow positioning the EditText
appropriately without using a horizontal LinearLayout
? Otherwise, I think I'll have to write a RadioButton
subclass which ought to be a real party!
推荐答案
您应该能够改变你的父布局,以
RelativeLayout的
。然后给你的 RadioGroup中
You should be able to change your parent layout to
RelativeLayout
. Then give your RadioGroup
android:orientation="vertical"
然后把你的
的EditText
旁边和的 RadioGroup中
底部对齐。我还没有尝试过,但这样的事情应该工作。
then place your
EditText
next to and aligned with the bottom of the RadioGroup
. I haven't tried it but something like this should work.
更新
我有时间来检验。这似乎给您正在寻找
I got time to test it. This seems to give the output you are looking for
<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>
请注意,我也改变了一些
宽度
S与 WRAP_CONTENT
所以它会结合在一起。更改定位
的 RadioGroup中
垂直省去了在的LinearLayout
Note that I also changed some of the
width
s to wrap_content
so it would fit together. Changing the orientation
of the RadioGroup
to vertical eliminates the need for the LinearLayout
这篇关于添加的EditText单选按钮旁边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!