我的问题是我想要一个包含3个单选按钮的单选组,如下所示:
三个选项分别是:
1. [] 男性
2. [] 女性
3. [] 自定义:(自我描述的身份)
然而,我的问题在于我希望用户可以在EditText中键入他们的自我描述身份,以便我进行检索。
以下代码来自我的XML页面,其中一些元素被“####”阻止。
然而,我的问题在于我希望用户可以在EditText中键入他们的自我描述身份,以便我进行检索。
以下代码来自我的XML页面,其中一些元素被“####”阻止。
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="####"
android:id="@+id/male_female_custom_choice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton android:id="@+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button_male"
android:checked="true" />
<RadioButton android:id="@+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button_female"
android:checked="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="####"
android:weightSum="1">
<RadioButton
android:id="@+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_button_custom"
android:checked="false" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="####"
android:hint="####"
android:focusableInTouchMode="true"
android:gravity="center"
android:layout_weight="1.05"
android:textSize="14sp" />
<TextView
android:layout_width="42dp"
android:layout_height="43dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="####"
android:id="####"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="0dp"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
如您所见,我已尝试使用LinearLayout来隔离自定义选项。
然而,存在意外和不希望的副作用。
- 自定义选项可以与其他两个预定义性别一起选择。
- 自定义选项不能单独选择。
在活动的实际Java文件中,我有以下代码:
// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
pat.gender = "male";
}
if (selectedId == 2) {
pat.gender = "female";
}
if (selectedId == 3) {
mEdit = (EditText)findViewById(R.id.####);
pat.gender = (mEdit.getText().toString());
}
由于我对Java有些生疏,所以可能会出现一些新手错误。请建议。
再次说明,我正在寻找一种方法来获取一个由3个单选按钮组成的集合,每个单选按钮都位于单独的一行,最后一个单选按钮旁边是一个EditText,在其中获取所需信息。
编辑:这里是我想要它看起来的图片: (http://i68.tinypic.com/ao2oow.png)
不幸的是,我需要10个声誉才能发布图片。:(
Mohit的答案将EditText放在与自定义输入不同的一行。 (http://i63.tinypic.com/68u88x.png)
请注意,EditText的方向与自定义相邻,而不是下方。对于我没有足够清楚地说明我想要什么,我深感抱歉。