我已经搜索了类似问题的答案,但无法找到答案。
我似乎无法从我的Java代码中调用subwayMcDClick
。我不知道括号中要放什么(View view)
。这是我的代码:
public void submitAnswer(View view) {
// 1) Which has the most calories? Subway vs. McD's
subwayMcDClick();
// 4) Which two have the most caffeine?
CheckBox brewedCoffeeCheckbox = findViewById(R.id.brewed_coffee_checkbox);
boolean brewedCoffeeCaffeine = brewedCoffeeCheckbox.isChecked();
CheckBox brewedBlackTeaCheckbox = findViewById(R.id.brewed_black_tea_checkbox);
boolean brewedBlackTea = brewedBlackTeaCheckbox.isChecked();
CheckBox colaCheckbox = findViewById(R.id.cola_checkbox);
boolean cola = colaCheckbox.isChecked();
CheckBox rootBeerCheckbox = findViewById(R.id.root_beer_checkbox);
boolean rootBeer = rootBeerCheckbox.isChecked();
if (brewedCoffeeCaffeine && brewedBlackTea) {
caffeine_4 = true;
} else caffeine_4 = false;
String answerMessage = createAnswerSummary(caffeine_4);
displayMessage(answerMessage);
}
public void subwayMcDClick(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch (view.getId()) {
case R.id.subway_vegetarian_button:
if (checked)
calories_1 = true;
case R.id.mcd_big_mac_button:
if (checked)
calories_1 = false;
}
}
答案 0 :(得分:0)
只需将复选框作为参数发送, 假设'cb'是您想要获得
值的复选框CheckBox cb = new CheckBox(this);
subwayMcDClick(cb);
答案 1 :(得分:0)
首先在XML布局中添加一个RadioGroup,如下所示
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_below="@+id/imageView"
android:layout_marginTop="58dp"
android:weightSum="1"
android:id="@+id/radioGroup"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2"
android:layout_alignRight="@+id/textView3"
android:layout_alignEnd="@+id/textView3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="SUBWAY_VEGETARIAN"
android:id="@+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="McDonalds"
android:id="@+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:layout_weight="0.13" />
</RadioGroup>
然后,在你的Activity中使用它就像这样
public class MainActivity extends Activity {
private RadioGroup radioGroup;
private RadioButton radioButton;
private Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
//i assume you have a submit button
submit =(Button)findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedId=radioGroup.getCheckedRadioButtonId();
if(selectedId==R.id.radioButton){
//do something when subway is selected
}
else if(selectedId==R.id.radioButton2){
//do something when McD is selected
}
}
});
}
}