我被困在简单的代码中。我正在尝试检查哪个Radiobutton
已选中,然后在Button
点击上显示结果。当我点击男性单选按钮时,它显示结果完美但当我检查女性单选按钮然后它没有显示或你可以说它不工作,甚至没有显示任何错误。我不知道我的代码有什么问题。请看一下:
RadioButton radm,radf;
radm=(RadioButton)findViewById(R.id.radiomale);
radf=(RadioButton)findViewById(R.id.radiofemale);
Button find=(Button) findViewById(R.id.btnfind);
//Where s is Spinner and find is Button
find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(s.getSelectedItem().toString().equals("KG")){
if(radm.isChecked()){
int inches= myfoot*12+myinch;
float sizeincm= inches*2.54f;
mybmr= (float) (66.47f+ (13.7*myweight) +(5*sizeincm) -6.8 * myage);
myrmr= (float) ((10*myweight)+(6.25*sizeincm)-(5*myage)+5);
mygcal=level.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this, Output_activity.class);
Bundle extras = new Bundle();
extras.putFloat("bmr",mybmr);
extras.putFloat("rmr",myrmr);
extras.putString("gcal",mygcal);
intent.putExtras(extras);
startActivity(intent);
}
}
else if(s.getSelectedItem().toString().equals("KG")){
if(radf.isChecked()) {
int inches = myfoot * 12 + myinch;
float sizeincm = inches * 2.54f;
mybmr = (float) (655.1f + (9.6 * myweight) + (1.8 * sizeincm) - 4.7 * myage);
myrmr = (float) ((10 * myweight) + (6.25 * sizeincm) - (5 * myage) + 5);
mygcal = level.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this, Output_activity.class);
Bundle extras = new Bundle();
extras.putFloat("bmr", mybmr);
extras.putFloat("rmr", myrmr);
extras.putString("gcal", mygcal);
intent.putExtras(extras);
startActivity(intent);
}
}
}
});
这是我的XML:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/radmf"
android:padding="4dp"
android:orientation="vertical">
<RadioButton
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="Male"
android:checked="true"
android:background="@drawable/customedittext"
android:id="@+id/radiomale"/>
<View
android:layout_width="match_parent"
android:layout_height="5dp"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="Female"
android:background="@drawable/customedittext"
android:id="@+id/radiofemale"/>
</RadioGroup>
答案 0 :(得分:1)
我解决了我的问题:
if(s.getSelectedItem().toString().equals("KG") && radm.isChecked()){
int inches= myfoot*12+myinch;
float sizeincm= inches*2.54f;
mybmr= (float) (66.47f+ (13.7*myweight) +(5*sizeincm) -6.8 * myage);
myrmr= (float) ((10*myweight)+(6.25*sizeincm)-(5*myage)+5);
mygcal=level.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this, Output_activity.class);
Bundle extras = new Bundle();
extras.putFloat("bmr",mybmr);
extras.putFloat("rmr",myrmr);
extras.putString("gcal",mygcal);
intent.putExtras(extras);
startActivity(intent);
}
else if(s.getSelectedItem().toString().equals("KG") && radf.isChecked()){
int inches = myfoot * 12 + myinch;
float sizeincm = inches * 2.54f;
mybmr = (float) (655.1f + (9.6 * myweight) + (1.8 * sizeincm) - 4.7 * myage);
myrmr = (float) ((10 * myweight) + (6.25 * sizeincm) - (5 * myage) + 5);
mygcal = level.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this, Output_activity.class);
Bundle extras = new Bundle();
extras.putFloat("bmr", mybmr);
extras.putFloat("rmr", myrmr);
extras.putString("gcal", mygcal);
intent.putExtras(extras);
startActivity(intent);
}
仍感谢安德斯的时间。感谢
答案 1 :(得分:0)
像这样:
RadioButton radm,radf;
radm=(RadioButton)findViewById(R.id.radiomale);
radf=(RadioButton)findViewById(R.id.radiofemale);
Button find=(Button) findViewById(R.id.btnfind);
//Where s is Spinner and find is Button
find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(s.getSelectedItem().toString().equals("KG")){
if(radm.isChecked()){
int inches= myfoot*12+myinch;
float sizeincm= inches*2.54f;
mybmr= (float) (66.47f+ (13.7*myweight) +(5*sizeincm) -6.8 * myage);
myrmr= (float) ((10*myweight)+(6.25*sizeincm)-(5*myage)+5);
mygcal=level.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this, Output_activity.class);
Bundle extras = new Bundle();
extras.putFloat("bmr",mybmr);
extras.putFloat("rmr",myrmr);
extras.putString("gcal",mygcal);
intent.putExtras(extras);
startActivity(intent);
}else if (radf.isChecked()){
int inches = myfoot * 12 + myinch;
float sizeincm = inches * 2.54f;
mybmr = (float) (655.1f + (9.6 * myweight) + (1.8 * sizeincm) - 4.7 * myage);
myrmr = (float) ((10 * myweight) + (6.25 * sizeincm) - (5 * myage) + 5);
mygcal = level.getSelectedItem().toString();
Intent intent = new Intent(MainActivity.this, Output_activity.class);
Bundle extras = new Bundle();
extras.putFloat("bmr", mybmr);
extras.putFloat("rmr", myrmr);
extras.putString("gcal", mygcal);
intent.putExtras(extras);
startActivity(intent);
}
}
}
});