2013-04-05 148 views
0

我有一個程序,從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

回答

0

你的錯誤是帕拉姆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