Java 在类似android的单选按钮中选择性别

Java 在类似android的单选按钮中选择性别,java,android,radio-button,Java,Android,Radio Button,我正在制作一个简单的程序,我希望能够像单选按钮一样选择我的性别,你要么选择女性,要么选择男性,另一个选择应该是不选中的。我有单独的按钮图像,按下和未按下 我已将状态存储在选择器文件中: 按钮\u female.xml: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_focused="true" android:drawable="@drawabl

我正在制作一个简单的程序,我希望能够像单选按钮一样选择我的性别,你要么选择女性,要么选择男性,另一个选择应该是不选中的。我有单独的按钮图像,按下和未按下

我已将状态存储在选择器文件中:

按钮\u female.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/gender_f"/>
<item android:state_pressed="true" android:drawable="@drawable/gender_f" />
<item android:drawable="@drawable/gender_f_notpressed" />
</selector>
    <Button
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:id="@+id/maleButton"
        android:layout_alignParentTop="true"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="220dp"
        android:drawable="@drawable/button_male"
        android:onClick="onGenderButtonClicked"
        />
但是,由于以下几个原因,这不起作用

1:按下或未按下按钮上的图片都不会显示

2:我的代码不正确,按钮不能按预期工作。我想让他们表现得像一个广播组(你可以选择男性或女性作为你的性别)


所以我这里似乎有两个问题。有人知道解决这个问题的好方法吗?我对编写android应用程序非常陌生。

您可以使用xml格式的radioGroup,如下所示:

<RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="31dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_female"
            android:checked="true"
            android:text="male" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_female"
            android:text="female" />
    </RadioGroup>

这里有一个很好的例子来说明您的需要:

布局:

 <RadioGroup
    android:id="@+id/radioSex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_male" 
        android:checked="true" />

    <RadioButton
        android:id="@+id/radioFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_female" />

</RadioGroup>

如果你需要这样的东西:

您可以使用以下布局:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    >
        <RadioGroup
            android:id="@+id/radioGrp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/gender_old"
            android:paddingTop="64dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            >
            <RadioButton
                android:id="@+id/radioM"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:checked="true"
                android:drawableRight="@drawable/male"
                android:layout_weight="1"
                android:textSize="14dp"
                android:text="Male"
            />
            <RadioButton
                android:id="@+id/radioF"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:checked="false"
                android:drawableRight="@drawable/female"
                android:layout_weight="1"
                android:textSize="14dp"
                android:text="Female"
            />
        </RadioGroup>
</RelativeLayout>


请检查这两个示例的相同情况:,您也可以选择segmented button@user2450263谢谢,但您提到的示例没有提到使用图像按钮。我把单选按钮弄得乱七八糟,但我希望我自己设计的一些按钮能像单选按钮一样工作。
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}

public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);

btnDisplay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);

Toast.makeText(MyAndroidAppActivity.this, radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    >
        <RadioGroup
            android:id="@+id/radioGrp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/gender_old"
            android:paddingTop="64dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            >
            <RadioButton
                android:id="@+id/radioM"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:checked="true"
                android:drawableRight="@drawable/male"
                android:layout_weight="1"
                android:textSize="14dp"
                android:text="Male"
            />
            <RadioButton
                android:id="@+id/radioF"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:checked="false"
                android:drawableRight="@drawable/female"
                android:layout_weight="1"
                android:textSize="14dp"
                android:text="Female"
            />
        </RadioGroup>
</RelativeLayout>