Android Studio 单选按钮在选中时未读取为已选中

Android Studio Radio Button not read as selected when selected

我正在使用 if 语句来检查单选按钮组中两个单选按钮中的哪一个被选中,但无论如何应用程序始终认为第二个被选中。我不明白为什么。

@TargetApi(24)
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.drawable.ic_launcher_carwash);
        getSupportActionBar().setDisplayUseLogoEnabled(true);

        final EditText washNumInput = (EditText)findViewById(R.id.txtWashNum);
        final RadioButton extOnly = (RadioButton)findViewById(R.id.radBtnExtOnly);
        final RadioButton extAndInt = (RadioButton)findViewById(R.id.radBtnExtAndInt);
        final Button btnCalc = (Button)findViewById(R.id.btnCalc);
        final TextView output = (TextView)findViewById(R.id.txtOutput);

        btnCalc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int washes = Integer.parseInt(washNumInput.getText().toString());
                double cost = 0.0;
                DecimalFormat currency = new DecimalFormat("$###,###.##");
                if (washes < 12){
                    Toast.makeText(MainActivity.this,"No discount for less than 12 washes",
                            Toast.LENGTH_LONG).show();
                    if (extOnly.isSelected()){
                        cost += washes * 10.99;
                        output.setText(currency.format(cost) + " for " + washes + " washes");
                    }
                    else{
                        cost += washes * 15.99;
                        output.setText(currency.format(cost) + " for " + washes + " washes");
                    }
                }
                else{
                    if (extOnly.isSelected()){
                        cost += washes * 8.99;
                        output.setText(currency.format(cost) + " for " + washes + " washes");
                    }
                    else{
                        cost += washes * 12.99;
                        output.setText(currency.format(cost) + " for " + washes + " washes");
                    }
                }
            }
        });
    }
}

这里是 XML 文件以防万一:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.travis.carwashapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/car_wash_packages"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.032"
        android:id="@+id/textView2"
        app:layout_constraintHorizontal_bias="0.515" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/discount_params"
        android:textColor="@android:color/darker_gray"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.104" />

    <EditText
        android:id="@+id/txtWashNum"
        android:layout_width="358dp"
        android:layout_height="43dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="@string/washnumber"
        android:inputType="textPersonName"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.196" />

    <RadioGroup
        android:id="@+id/radGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:checkedButton="@+id/radBtnExtOnly"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.33">

        <RadioButton
            android:id="@+id/radBtnExtOnly"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:text="@string/exterior_only"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.257"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.306" />

        <RadioButton
            android:id="@+id/radBtnExtAndInt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:layout_weight="1"
            android:text="@string/exterior_with_interior_vacuum"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" />
    </RadioGroup>

    <Button
        android:id="@+id/btnCalc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="@string/calculate_package"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.502"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="320dp"
        android:layout_height="167dp"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/app_name"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.516"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/carwash"
        tools:layout_editor_absoluteY="328dp" />

    <TextView
        android:id="@+id/txtOutput"
        android:layout_width="377dp"
        android:layout_height="35dp"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_light"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.571"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.604" />

</android.support.constraint.ConstraintLayout>

尝试使用 isChecked() and not isSelected()。您的代码总是假设检查第二个 RadioButton 的原因是因为 isSelected() returns false,不断落入条件语句的 else 情况;你在哪里处理第二个按钮!

如果您使用多个 if 条件而不是 if/else 单独检查每个按钮的状态,这种行为会更加明显;因为 RadioGroup 总是保证只能选择一个选项。