Android单选按钮组内嵌EditText

3
我的问题是我想要一个包含3个单选按钮的单选组,如下所示: 三个选项分别是: 1. [] 男性 2. [] 女性 3. [] 自定义:(自我描述的身份)
然而,我的问题在于我希望用户可以在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来隔离自定义选项。

然而,存在意外和不希望的副作用。

  1. 自定义选项可以与其他两个预定义性别一起选择。
  2. 自定义选项不能单独选择。

在活动的实际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的方向与自定义相邻,而不是下方。对于我没有足够清楚地说明我想要什么,我深感抱歉。


2
链接或代码在哪里? - Surabhi Singh
https://docs.google.com/document/d/1IwIErvZ7NBiqZ3e6ZHqQn_ajUihU3GB7gfCHUhSuDbQ/edit?usp=sharing - DLuciferin
https://docs.google.com/document/d/1gwBU031O1y6hGCjnc3hdrdLz93gpGAZ0iIYROqnrrkg/edit?usp=sharing - DLuciferin
最初禁用编辑文本... 当有人从单选组中选择自定义选项时,启用编辑文本。 - Surabhi Singh
请查看此链接:http://stackoverflow.com/a/35239621/2826147 - Amit Vaghela
2个回答

4
  1. 因为selectedId的值不会是1、2或3……调试一下,你就能得到值。

不能仅选择自定义选项。

  1. LinearLayout中移除第3个RadioButton,将其替换为第2个RadioButton下方,并将EditTextTextView放在LinearLayout内。

在你的监听器上使用getCheckedRadioButtonId和该RadioButtongetText(),并进行相应的检查……

我不知道你的任务是什么,但以下是如何使所有三个RadioButton正常工作并获取自定义文本的方法……

xml…

更新

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ex.MainActivity" >

 <RadioGroup
    android:id="@+id/male_female_custom_choice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radio_button_male"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male" />

    <RadioButton
        android:id="@+id/radio_button_female"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="FeMale" />

    <RadioButton
        android:id="@+id/radio_button_custom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="false"
        android:text="Custom" />
</RadioGroup>

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/male_female_custom_choice"
    android:layout_toRightOf="@+id/male_female_custom_choice"
    android:orientation="horizontal"
    android:weightSum="1" >

    <EditText
        android:id="@+id/edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".8"
        android:ems="10"
        android:focusableInTouchMode="true"
        android:gravity="center"
        android:hint="aa"
        android:inputType="text"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="42dp"
        android:layout_height="43dp"
        android:layout_marginLeft="0dp"
        android:layout_weight=".2"
        android:gravity="center"
        android:singleLine="true"
        android:text="aaa"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />
 </LinearLayout>

 <Button
    android:id="@+id/but"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/male_female_custom_choice"
    android:text="Get" />

</RelativeLayout>

Java文件..

public class MainActivity extends Activity {

 public EditText mEdit;
 public RadioButton rButton;
 public RadioGroup rSexGroup;
 public Button but;
 public String str = "";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
    but= (Button) findViewById(R.id.but);

    but.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int selectedId = rSexGroup.getCheckedRadioButtonId();
            rButton = (RadioButton)findViewById(selectedId);
            if (rButton.getText().toString().equals("Male")) {
                str = "Male";
            }
            if (rButton.getText().toString().equals("FeMale")) {
                str = "FeMale";
            }
            if (rButton.getText().toString().equals("Custom")) {
                mEdit = (EditText)findViewById(R.id.edit);
                str = mEdit.getText().toString();
            }
            Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
        }
    });
  }
}

你也可以设置LinearLayout的可见性,以便仅在自定义选中时可见... 希望它有帮助...

RelativeLayout的问题似乎在于我无法将EditText放在与“Custom” RadioButton相同的行上。我正在寻找一种将它们放在同一行的方法。 - DLuciferin
是的,那是正确的,但TextView会强制使EditText缺少空间。更准确地说,即使我将文本大小修改为小号,我仍然会遇到onMeasure错误。 - DLuciferin
您可能有大量的文本。 - Iamat8
如果我能让LinearLayout小一点,也许就能塞进去了。 - DLuciferin
是的,我现在得到了我想要的东西,谢谢你的帮助和耐心! - DLuciferin
显示剩余4条评论

1
  • put radio group in RelativeLayout
  • set third radiobutton text as empty/null
  • add EditText as layout_alignParentBottom="true"
  • now programmatically call EditText's onFocusChangeListener

    edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    
        @Override
    
        public void onFocusChange(View view, boolean b) {
            if(b)
              thirdRadio.setCheched(true);
        }
    });
    

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