我使用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>
答案 0 :(得分:0)
尝试使用isChecked()
和不 isSelected()
。我们的代码总是假设第二个RadioButton
被检查的原因是因为isSelected()
返回false
,不断陷入条件语句的else
情况;你在哪里处理第二个按钮!
如果您使用多个if
条件而不是if
/ else
单独检查每个按钮的状态,则此行为会更加明显;因为RadioGroup
总是保证只能选择一个选项。