簡體   English   中英

選中時,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()不是 isSelected() 您的代碼始終假定第二個RadioButton被選中的原因是, isSelected()返回false ,不斷陷入條件語句的else情況; 您在哪里處理第二個按鈕!

如果您使用多個if條件而不是if / else來單獨檢查每個按鈕的狀態,則此行為將更加明顯。 因為RadioGroup始終保證只能選擇一個選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM