2017-05-27 6 views
0

Ich verwende eine if-Anweisung, um zu überprüfen, welche von zwei Optionsfeldern in einer Radiogruppe ausgewählt ist, aber die App denkt immer, dass die zweite ausgewählt ist. Ich kann nicht herausfinden warum.Android Studio Optionsschaltfläche wurde nicht ausgewählt, als ausgewählt

@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"); 
        } 
       } 
      } 
     }); 
    } 
} 

ist die XML-Datei, falls das hilft:

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

Antwort

0

Versuchen isChecked() und nichtisSelected() verwenden. Der Grund, warum Ihr Code immer davon ausgeht, dass die zweite RadioButton überprüft wird, ist, weil isSelected()false zurückgibt, die ständig in den else Fall Ihrer bedingten Aussage fallen; wo du den zweiten Knopf hälst!

Dieses Verhalten wäre etwas offensichtlicher, wenn Sie den Status jeder Taste einzeln überprüfen, indem Sie mehrere if Bedingungen statt if/else; da die RadioGroup immer garantiert, kann nur eine einzige Option ausgewählt werden.

+0

Das hat funktioniert, vielen Dank. Die feinen Unterschiede zwischen dem Java, das mit Android Studio verwendet wird, und anderen IDEs wie Eclipse können manchmal sehr frustrierend sein. – MWTravesty

+0

@MWTravesty Es ist nur eines dieser Dinge. Lass dich nicht davon abbringen, es ist ein Fehler, den du nie wieder machen wirst. –

Verwandte Themen