在Android中像单选按钮一样选择性别

3

我正在制作一个简单的程序,希望能够像单选按钮一样选择我的性别,你要么选择女性,要么选择男性,另一个选择应该取消选中状态。我有分别为两个按钮准备的不同图像,包括按下和未按下的状态。

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

button_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>

layout/settings_activity.xml 文件中:

    <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"
        />

在活动中,onClick 写成了:
   public void onGenderButtonClicked(View view) {
        if(feButton.isPressed()){
            maButton.setEnabled(false);
            radioPressed = true;
        } else if (maButton.isPressed()){
            feButton.setEnabled(false);
            radioPressed = true;
        } else {
            radioPressed = false;
        }

    }

然而,这种方法出现了几个问题。 问题1:按钮上的图片无法显示,按下和未按下都是如此。 问题2:我的代码很糟糕,按钮的功能不如预期。我希望它们表现得像一个单选组(你要么选择男性,要么选择女性)。
所以我似乎有两个问题。有谁知道解决这个问题的好方法吗?我对编写android应用程序非常陌生。

1
请查看以下两个示例,以了解相同情况:onetwo - Pararth
你也可以考虑使用分段按钮,这里有一个链接:https://github.com/vinc3m1/android-segmentedradiobutton - Jitesh Upadhyay
@user2450263 谢谢你,但是你提到的例子没有提及如何使用图像按钮。我已经尝试过可行的单选按钮,但我想让我自己设计的一些按钮能够像单选按钮一样工作。 - enrm
3个回答

8
如果你需要像这样的东西: enter image description here 你可以使用这个布局:
<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>

5
您在这里看到的是一个很好的示例,与您所需内容相似:

http://www.mkyong.com/android/android-radio-buttons-example/

布局:

 <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>

对于控制:

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();
}
});
}

1
你可以在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>

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