Java 嵌入EditText的Android单选按钮组

Java 嵌入EditText的Android单选按钮组,java,android,Java,Android,我的问题是,我想要一个有3个单选按钮的单选组,使用下面的方案。 这三种选择是: 1.[]男性 2.[]女性 3.[]自定义:(自我描述的身份) 但是,问题是,我希望用户在EditText中键入自己描述的身份,以便我检索 下面的代码来自我的XML页面,其中一些元素被“#####”屏蔽 因为我对Java有点生疏,所以我可能有一些真正的新手错误。请告知 再一次,我在寻找一种方法来获得一组3个单选按钮,每个单选按钮在一行上,最后一个单选按钮旁边有一个EditText,我可以从中获得所需的信息 编辑:以下

我的问题是,我想要一个有3个单选按钮的单选组,使用下面的方案。 这三种选择是: 1.[]男性 2.[]女性 3.[]自定义:(自我描述的身份)

但是,问题是,我希望用户在EditText中键入自己描述的身份,以便我检索

下面的代码来自我的XML页面,其中一些元素被“#####”屏蔽

因为我对Java有点生疏,所以我可能有一些真正的新手错误。请告知

再一次,我在寻找一种方法来获得一组3个单选按钮,每个单选按钮在一行上,最后一个单选按钮旁边有一个EditText,我可以从中获得所需的信息

编辑:以下是我希望它看起来像什么的图片: ()

不幸的是,我需要10个声誉来发布图片:(

Mohit的答案在不同于自定义输入的行上给出EditText。 ()

请注意,编辑文本的方向与自定义方向相邻,而不是在下方。很抱歉,我没有明确说明我想要的内容。

  • 将无线电组放入
    RelativeLayout
  • 将第三个单选按钮设置为空/空
  • 添加
    EditText
    作为
    layout\u alignParentBottom=“true”
  • 现在以编程方式调用EditText的
    onFocusChangeListener

    edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    
        @Override
    
        public void onFocusChange(View view, boolean b) {
            if(b)
              thirdRadio.setCheched(true);
        }
    });
    
  • 因为
    selectedId
    将不是1、2或3…调试它,您将获得值
  • 无法单独选择自定义选项

  • LinearLayout
    中移除第三个
    单选按钮
    ,并将第二个
    单选按钮
    下方替换,然后将
    编辑文本
    文本视图
    放入
    线性布局
  • 在侦听器上,获取该
    单选按钮的
    getCheckedRadioButtonId
    getText()
    ,并相应地进行检查

    我不知道你的任务是什么,但这里是如何让所有三个
    单选按钮
    工作并获得自定义文本

    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>
    
    您还可以设置
    LinearLayout
    的可见性,使其仅在选中“自定义”时可见


    希望有帮助。

    链接或代码在哪里?最初禁用编辑文本。.当有人从无线电组中选择自定义选项启用编辑文本检查此项时,我可以理解java代码背后的原因,这非常有帮助。但是,XML代码不适合我的目的(我附加了一些图片来显示我想要的内容,并与XML代码中显示的内容进行了比较)。是否有办法调整代码并移动元素,使其更像我想要的显示?RelativeLayout的问题似乎是我无法将EditText与“Custom”放在同一行上RadioButton。我正在寻找一种方法将它们放在同一条线上。你可以实现你想要的(图片中)通过将LinearLayout置于Radiobutton的右侧并与底部对齐…是的,谢谢。我用复选标记了它。我有一个不同的问题,尽管这是相关的…如果我在男/女选项中添加更多的信息/文本,为EditText提供的空间会减少,这是我真正不想要的。[]男(***************************信息************************)是的,这是正确的,但文本视图强制编辑文本缺少空间。准确地说,即使我将文本大小修改为较小,我也会收到一个onMeasure错误。
    edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    
        @Override
    
        public void onFocusChange(View view, boolean b) {
            if(b)
              thirdRadio.setCheched(true);
        }
    });
    
    <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>
    
    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();
            }
        });
      }
    }