0

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

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);

        }

    };
}
4

1 回答 1

0

您的错误是参数是对内部checkedId检查的标识符的引用(请参阅文档),因此您的代码将如下所示:RadioButtonRadioGroup

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;
            }
        }
    });
于 2013-04-05T18:08:21.033 回答