与日期选择器的RadioButton选择

问题描述:

我有一个程序,从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

看回答下面 – Houcine 2013-04-05 18:16:25

+0

就像我在你的其他问题的答案,我强烈建议你去100%阿贾克斯。 'WebMethod'非常简单。您只需使用jQuery'$ .ajax()'发送数据,在'WebMethod'中处理数据,并将数据发送回客户端。它更直接,更逻辑(更重要的是功能),而且你不会把自己装进去。在这里查看一个好教程的“最受欢迎”http://encosia.com/ – 2013-09-01 17:52:51

你的错误是帕拉姆checkedId是引荐到检查RadioButtonRadioGroup内identifiant(指的docs),所以您的代码将是这样的:

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; 
      } 
     } 
    }); 
+0

嗨,谢谢你的快速响应。该程序仍然停止。我把radBasic和radDeluxe代替了btnBasic和btnDeluxe,因为这些都是我按钮的id。 – Lolo 2013-04-05 18:28:56

+0

你有什么异常?从logcat添加异常的堆栈跟踪 – Houcine 2013-04-05 21:44:39

+0

再次感谢您的帮助。我改变了,并使用.isChecked单独的radioButtons,并在底部的OnDateSet方法中使用了if语句。非常感谢!! – Lolo 2013-04-07 17:12:14