嗯,问题是......我的代码应该从用户点击的单选按钮输出金额,但它根本没有输出金额...我真的不确定问题是什么。我创建了一个radiogroup并使用if语句来确定是否检查了radiobutton。我认为问题在于计算金额。 我需要制作一个按钮吗?我认为当点击radiobutton时,它会自动使用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 CODE:
<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>
答案 0 :(得分:1)
您需要一个事件来触发单选按钮的检查。目前,它只是检查onCreate中的if语句,就是它(如果您默认选中了radiobutton,则会在此处设置一个值)。我建议将一个监听器附加到整个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)
}
});