0

所以,基本上这很简单。我正在创建一个小费计算器应用程序,它允许用户输入一堆关于他们的膳食/服务的信息,我应该在计算按钮旁边的文本字段底部计算结果。我遇到的麻烦是从“选择提示:”选项中只选择一个单选按钮。我尝试将 RadioGroup 小部件添加到我的应用程序,但我不知道如何使我的单选按钮符合它,也不知道如何在我的应用程序中添加单选组而不影响我当前单选按钮的大小和位置. 这是我的应用程序的外观:http ://tinypic.com/r/yc46t/8

我的活动.java

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        final EditText enteredFoodAmount = (EditText)findViewById(R.id.foodAmount);
        final EditText enteredDrinkAmount = (EditText)findViewById(R.id.drinkAmount);
        final EditText enteredTaxAmount = (EditText)findViewById(R.id.taxAmount);
        final RadioButton tenSelected = (RadioButton)findViewById(R.id.tenPercent);
        final RadioButton fifteenSelected = (RadioButton)findViewById(R.id.fifteenPercent);
        final RadioButton twentySelected = (RadioButton)findViewById(R.id.twentyPercent);
        final TextView displayedFoodAmount = (TextView)findViewById(R.id.theFoodAmount);
        final TextView displayedDrinkAmount = (TextView)findViewById(R.id.theDrinkAmount);
        final TextView displayedTaxAmount = (TextView)findViewById(R.id.theTaxAmount);
        final TextView displayedTipAmount = (TextView)findViewById(R.id.theTipAmount);
        final Button calculateButton = (Button)findViewById(R.id.calculate);
        final TextView displayedCalculation = (TextView)findViewById(R.id.calculatedAmount);
        final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.myRadioGroup);

        calculateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                double food = Double.parseDouble(enteredFoodAmount.getText().toString());
                double drink = Double.parseDouble(enteredDrinkAmount.getText().toString());
                double tax = Double.parseDouble(enteredTaxAmount.getText().toString()) / 100.0;
                double tip;
                double calculation;

                if (tenSelected.isChecked()){
                    twentySelected.setChecked(false);
                    tip = 0.10;
                    displayedTipAmount.setText(Double.toString(tip * 100.0) + "%");

                    displayedFoodAmount.setText("$" + Double.toString(food));
                    displayedDrinkAmount.setText("$" + Double.toString(drink));
                    displayedTaxAmount.setText(Double.toString(tax * 100.0) + "%");

                    double myTax = (food + drink) * tax;
                    double myTip = (food + drink) * tip;
                    calculation = (food + drink) + myTax + myTip;
                    displayedCalculation.setText("$" + Double.toString(calculation));
                }
                else if (twentySelected.isChecked()){
                    tenSelected.setChecked(false);
                    tip = 0.20;
                    displayedTipAmount.setText(Double.toString(tip * 100.0) + "%");

                    displayedFoodAmount.setText("$" + Double.toString(food));
                    displayedDrinkAmount.setText("$" + Double.toString(drink));
                    displayedTaxAmount.setText(Double.toString(tax * 100.0) + "%");

                    double myTax = (food + drink) * tax;
                    double myTip = (food + drink) * tip;
                    calculation = (food + drink) + myTax + myTip;
                    displayedCalculation.setText("$" + Double.toString(calculation));
                }
                else if (fifteenSelected.isChecked()){

                    tip = 0.15;
                    displayedTipAmount.setText(Double.toString(tip * 100.0) + "%");

                    displayedFoodAmount.setText("$"+ Double.toString(food));
                    displayedDrinkAmount.setText("$" + Double.toString(drink));
                    displayedTaxAmount.setText(Double.toString(tax * 100.0) + "%");

                    double myTax = (food + drink) * tax;
                    double myTip = (food + drink) * tip;
                    calculation = (food + drink) + myTax + myTip;
                    displayedCalculation.setText("$" + Double.toString(calculation));
                }

activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity"
    android:id="@+id/layoutt">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/food_total"
        android:id="@+id/foodTotal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/drink_total"
        android:id="@+id/drinkTotal"
        android:layout_below="@+id/foodTotal"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="38dp"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/foodAmount"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/drinkAmount"
        android:layout_alignBottom="@+id/drinkTotal"
        android:layout_alignLeft="@+id/foodAmount"
        android:layout_alignStart="@+id/foodAmount"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ten_percent"
        android:id="@+id/tenPercent"
        android:layout_toEndOf="@+id/drinkTotal"
        android:layout_above="@+id/fifteenPercent"
        android:layout_toRightOf="@+id/drinkTotal"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/fifteen_percent"
        android:id="@+id/fifteenPercent"
        android:layout_toEndOf="@+id/drinkTotal"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/tenPercent"
        android:layout_alignStart="@+id/tenPercent"
        android:layout_toRightOf="@+id/drinkTotal"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/twenty_percent"
        android:id="@+id/twentyPercent"
        android:layout_toEndOf="@+id/drinkTotal"
        android:layout_below="@+id/fifteenPercent"
        android:layout_alignLeft="@+id/fifteenPercent"
        android:layout_alignStart="@+id/fifteenPercent"
        android:layout_toRightOf="@+id/drinkTotal"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/calculate"
        android:id="@+id/calculate"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@+id/calculatedAmount"
        android:layout_alignBottom="@+id/calculate"
        android:layout_alignLeft="@+id/drinkAmount"
        android:layout_alignStart="@+id/drinkAmount"
        android:layout_alignTop="@+id/calculate"
        android:textSize="22sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Total Tax"
        android:id="@+id/tax"
        android:layout_marginTop="29dp"
        android:layout_below="@+id/drinkAmount"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:ems="10"
        android:id="@+id/taxAmount"
        android:layout_alignBottom="@+id/tax"
        android:layout_alignLeft="@+id/drinkAmount"
        android:layout_alignStart="@+id/drinkAmount"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/select_tip"
        android:id="@+id/selectTip"
        android:layout_above="@+id/tenPercent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/the_food"
        android:id="@+id/theFood"
        android:layout_marginTop="37dp"
        android:layout_below="@+id/twentyPercent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/the_drink"
        android:id="@+id/theDrink"
        android:layout_below="@+id/theFood"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="35dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/the_tax"
        android:id="@+id/theTax"
        android:layout_alignTop="@+id/theFood"
        android:layout_toRightOf="@+id/twentyPercent"
        android:layout_toEndOf="@+id/twentyPercent"
        android:layout_marginLeft="44dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="@string/the_tip"
        android:id="@+id/theTip"
        android:layout_toEndOf="@+id/twentyPercent"
        android:layout_alignTop="@+id/theDrink"
        android:layout_alignLeft="@+id/theTax"
        android:layout_alignStart="@+id/theTax"
        android:layout_toRightOf="@+id/twentyPercent"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/theFoodAmount"
        android:layout_toStartOf="@+id/calculatedAmount"
        android:layout_toRightOf="@+id/calculatedAmount"
        android:layout_toLeftOf="@+id/calculatedAmount"
        android:text="--"
        android:layout_alignTop="@+id/theFood"
        android:layout_alignLeft="@+id/theDrinkAmount"
        android:layout_alignStart="@+id/theDrinkAmount"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="--"
        android:id="@+id/theDrinkAmount"
        android:layout_toEndOf="@+id/theDrink"
        android:layout_alignTop="@+id/theDrink"
        android:layout_toRightOf="@+id/theDrink"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="--"
        android:id="@+id/theTaxAmount"
        android:layout_toEndOf="@+id/theTax"
        android:layout_alignTop="@+id/theTax"
        android:layout_toRightOf="@+id/theTax"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="--"
        android:id="@+id/theTipAmount"
        android:layout_toEndOf="@+id/theTip"
        android:layout_toLeftOf="@+id/theTip"
        android:layout_toStartOf="@+id/theTip"
        android:layout_toRightOf="@+id/theTip"
        android:layout_alignTop="@+id/theTip"
        android:layout_alignRight="@+id/calculatedAmount"
        android:layout_alignEnd="@+id/calculatedAmount"/>

    <RadioGroup
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignTop="@+id/tenPercent"
        android:layout_toRightOf="@+id/selectTip"
        android:layout_toEndOf="@+id/selectTip"
        android:layout_above="@+id/theFood"
        android:layout_toLeftOf="@+id/theTip"
        android:layout_toStartOf="@+id/theTip"
        android:id="@+id/myRadioGroup"
        android:baselineAligned="false">
    </RadioGroup>

</RelativeLayout>
4

1 回答 1

0

RadioButtons 必须是 RadioGroup 内的子项。

<RelativeLayout ... >

    <!-- some views -->

    <RadioGroup ... >
        <RadioButton ... />

        <RadioButton ... />

        <RadioButton ... />
    </RadioGroup>

    <!-- more views -->

</RelativeLayout>
于 2014-10-14T03:31:56.513 回答