我有一个从日期选择器获取日期的程序,还需要从单选按钮组中选择客舱。我知道我的错误在于单选按钮侦听器,也许我没有把它放在正确的位置。请记住,我是新来的!谢谢!
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);
}
};
}