我正在尝试使用switch语句用于radiobutton,但是当我运行它时我会出错。有人可以帮帮我。我已经粘贴了我的android代码。如果你知道答案,请帮忙。非常感谢。
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.R;
public class LunchtimeActivity extends Activity {
Restaurant r=new Restaurant();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save=(Button)findViewById(R.id.button1);
save.setOnClickListener(onSave);
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
EditText name=(EditText)findViewById(R.id.text1);
EditText address=(EditText)findViewById(R.id.text2);
r.setName(name.getText().toString());
r.setAddress(address.getText().toString());
RadioGroup types=(RadioGroup)findViewById(R.id.types);
switch (types.GetCheckedRadiobuttonId()) {
case R.id.Sit-Down:
r.setType("Sit_Down");
break;
case R.id.Take-Out:
r.setType("Take_Out");
break;
case R.id.Delivery:
r.setType("Delivery");
break;
}
}
};
}
答案 0 :(得分:1)
我可以看到以下问题:
GetCheckedRadiobuttonId
更改为getCheckedRadioButtonId
Sit-Down
无效的标识符。答案 1 :(得分:0)
我认为问题在于:
case R.id.Sit-Down:
case R.id.Take-Out:
不使用“ - ”重命名此ID。
答案 2 :(得分:0)
您需要捕获视图中的单选按钮以及无线电组
RadioButton sitdown = (RadioButton) findViewByid(R.id.Sit-Down);
等