我有一个程序可以从datepicker获取日期,还需要从单选按钮组中选择一个小屋。我知道我的错误在于radiobutton听众,也许我并没有把它放在正确的地方。请记住,我是新手!!谢谢!
public class Main extends Activity {
private int currentYear;
private int currentMonth;
private int currentDay;
static final int DATE_DIALOG_ID = 0;
private Button btDate;
private TextView reservation;
private String cabinChoice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btDate = (Button)findViewById(R.id.btnDate);
reservation = (TextView)findViewById(R.id.txtReservation);
btDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
//---RadioButton---
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
//---displays the ID of the RadioButton that is checked---
switch(checkedId){
case 0:
cabinChoice = "Basic";
break;
case 1:
cabinChoice = "Deluxe";
break;
}
}
});
final Calendar c = Calendar.getInstance();
currentYear = c.get(Calendar.YEAR);
currentMonth = c.get(Calendar.MONTH);
currentDay = c.get(Calendar.DAY_OF_MONTH);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected Dialog onCreateDialog(int id) {
switch (id){
case DATE_DIALOG_ID:
return new DatePickerDialog(this, reservationDate, currentYear, currentMonth, currentDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener reservationDate = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int month,
int day) {
// TODO Auto-generated method stub
reservation.setText("Your reservation is set for " + (month + 1)+("-") + day + ("-") + year
+ ("") + (" thru ") + ("") + (month + 1) + ("-") + (day + 2) + ("-") + year
+ ("in") + cabinChoice);
}
};
}
答案 0 :(得分:0)
您的错误是,参数checkedId
是RadioButton
内RadioGroup
中已检查radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
//---displays the ID of the RadioButton that is checked---
switch(checkedId){
case R.id.btnBasic:
cabinChoice = "Basic";
break;
case R.id.btnDeluxe:
cabinChoice = "Deluxe";
break;
}
}
});
的标识符的引用者(参考docs ),所以你的代码将是这样的:
{{1}}