0

好吧,问题是......我的代码应该从用户单击的单选按钮输出金额,但它根本没有输出金额......我真的不确定问题是什么。我创建了一个单选组并使用 if 语句来确定单选按钮是否被选中。我认为问题出在计算金额的某个地方。我需要做一个按钮吗?我认为它会在单击单选按钮时使用 if 语句自动计算金额。如果我需要一个按钮,我将如何创建一个?(我现在试图避免使用监听器)

我是 android/java 的菜鸟。如果有人能解释我的代码有什么问题,我将不胜感激。谢谢!

JAVA CODE 包com.Rox.crazyjoe;

import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends Activity {

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

    final double smallcst = 1.25;
        final double mediumcst = 2.00;
    final double largecst = 3.50;
    double amount;


    RadioButton small = (RadioButton) findViewById(R.id.radioSmall);
    RadioButton medium = (RadioButton) findViewById(R.id.radioMedium);
    RadioButton large = (RadioButton) findViewById(R.id.radioLarge);


    if (small.isChecked())
    {
        amount = smallcst;
    }
    else if(large.isChecked())
    {
        amount = largecst;
    }
    else if(medium.isChecked())
    {
        amount = mediumcst;
    }
        else
    {
        amount = 0;
    }

            DecimalFormat money = new DecimalFormat("$##.00");

    TextView t0 = (TextView)findViewById(R.id.textAmount);
    t0.setText("Total Amount: " + money.format(amount));

        }

       }

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:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >

  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="16dp"
    android:text="Size: "
    android:textAppearance="?android:attr/textAppearanceMedium" />

  <RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radioSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:text="Small" />

    <RadioButton
        android:id="@+id/radioMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioButton1"
        android:layout_below="@+id/radioButton1"
        android:text="Medium" />

    <RadioButton
        android:id="@+id/radioLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioMedium"
        android:layout_below="@+id/radioMedium"
        android:text="Large" />


  </RadioGroup>

  <TextView
    android:id="@+id/textAmount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="40dp"
    android:layout_marginRight="54dp"
    android:text="TextView" />

</RelativeLayout>
4

1 回答 1

1

您将需要一个事件来触发对单选按钮的检查。目前,它只是检查 onCreate 中的 if 语句,仅此而已(如果您默认选中了单选按钮,则会在此处设置一个值)。我建议将听众附加到整个 RadioGroup

radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) { 
            RadioButton radioButton = (RadioButton) findViewById(checkedId);
            //other logic here (like what do you want to do with this selected RadioButton)
        }
});
于 2013-10-12T15:03:24.887 回答