如何使Group Radio Button工作

时间:2014-06-01 16:52:00

标签: android radio-group

基本上我不知道如何让我的组单选按钮工作我已经尝试了.isChecked但是弹出错误信息,我该如何解决这个问题

公共类CC扩展了MainActivity {

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

    final EditText h = (EditText)findViewById(R.id.editText2);
    final EditText w = (EditText)findViewById(R.id.editText3);
    final EditText a = (EditText) findViewById(R.id.editText1);

    final TextView r = (TextView)findViewById(R.id.textView4);

    final TextView SUnder = (TextView) findViewById(R.id.suw);
    final TextView Under = (TextView) findViewById(R.id.uw);
    final TextView Normal = (TextView) findViewById(R.id.nor);
    final TextView Over = (TextView) findViewById(R.id.ow);
    final TextView MOver = (TextView) findViewById(R.id.mo);

    final TextView SUnderT = (TextView) findViewById(R.id.textView8);
    final TextView UnderT = (TextView) findViewById(R.id.textView9);
    final TextView NormalT = (TextView) findViewById(R.id.textView10);
    final TextView OverT = (TextView) findViewById(R.id.textView11);
    final TextView MOverT = (TextView) findViewById(R.id.textView12);

    final float result=0;

    final RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
    final RadioGroup male = (RadioGroup) findViewById(R.id.radio1);
    final RadioGroup female = (RadioGroup) findViewById(R.id.radio2);


    Button calculate = (Button) findViewById(R.id.button1);

    calculate.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            float height=0;
            float age = 0;
            float weight=0;
            float result=0;

            height= Float.parseFloat(h.getText().toString());
            age= Float.parseFloat(a.getText().toString());
            weight= Float.parseFloat(w.getText().toString());

以下代码是问题,我不知道如何解决它

            if (male.isChecked)  {

                result = (float) (66.47 + (13.75 * weight) + (5.0 * height) - (6.75 * age));

            }

            if (female.isChecked) {

                result = (float) (665.09 + (9.56 * weight) + (1.84 * height) - (4.67 * age));

            }


}

}}

2 个答案:

答案 0 :(得分:2)

malefemale应为RadioButton,而非RadioGroup。在您的XML中,它们都应该是外部RadioGroup的子项,以便选择一个取消选择另一个。

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

final RadioButton male = (RadioButton) findViewById(R.id.radio1);
final RadioButton female = (RadioButton) findViewById(R.id.radio2);

答案 1 :(得分:0)

首先,我假设男性和女性都是单选按钮而你不小心将它们宣布为无线电组。在这种情况下,您可以使用以下代码。

// Get selected radio button from radioGroup    
int selectedId = group.getCheckedRadioButtonId()
// Find the radiobutton by returned id
if(selectedId == R.id.radio1)
{
   result = (float) (66.47 + (13.75 * weight) + (5.0 * height) - (6.75 * age));
}else if(selectedId == R.id.radio2){
  result = (float) (665.09 + (9.56 * weight) + (1.84 * height) - (4.67 * age));
}