如何为两个按钮android创建一个状态

时间:2021-02-12 15:01:54

I have two Buttons, Male and Female, and what I want to do is, if the user select a Male button, change the background color, the way that it works as a RadioGroup, just can select Male or Female.

我有两个按钮,男性和女性,我想要做的是,如果用户选择一个男性按钮,更改背景颜色,它作为一个RadioGroup工作的方式,只需选择男性或女性。

 <LinearLayout
        android:paddingTop="20dp"
        android:layout_gravity="center"
        android:weightSum="2"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/btn_male"
            android:textColor="@color/White"
            android:background="@color/colorPrimaryDark"
            android:layout_gravity="end"
            android:text="@string/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="genderPerson"
            />

        <Button
            android:id="@+id/btn_female"
            android:textColor="@color/White"
            android:background="@color/colorPrimaryDark"
            android:text="@string/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="genderPerson"
            />
    </LinearLayout>

1 个解决方案

#1


1  

Usually for these kind of requirements i suggest you to use RadioButton instead of Buttons

通常对于这些要求,我建议你使用RadioButton而不是Buttons

you can achieve easily like below

你可以轻松实现如下

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pirates"
    android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ninjas"
    android:onClick="onRadioButtonClicked"/>
 </RadioGroup>

and in Activity

并在活动中

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radio_pirates:
        if (checked)
            // Pirates are the best
        break;
    case R.id.radio_ninjas:
        if (checked)
            // Ninjas rule
        break;
}
}

for more info refer here Documentation

有关更多信息,请参阅此处文档

this complete example example

这个完整的示例示例

#1


1  

Usually for these kind of requirements i suggest you to use RadioButton instead of Buttons

通常对于这些要求,我建议你使用RadioButton而不是Buttons

you can achieve easily like below

你可以轻松实现如下

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pirates"
    android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ninjas"
    android:onClick="onRadioButtonClicked"/>
 </RadioGroup>

and in Activity

并在活动中

public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();

// Check which radio button was clicked
switch(view.getId()) {
    case R.id.radio_pirates:
        if (checked)
            // Pirates are the best
        break;
    case R.id.radio_ninjas:
        if (checked)
            // Ninjas rule
        break;
}
}

for more info refer here Documentation

有关更多信息,请参阅此处文档

this complete example example

这个完整的示例示例