2014-03-18 82 views
0

在過去的24小時內,我一直在試圖找到關於我的問題的文章,但我仍然感到困惑。如果我錯過了曾經回答過這個問題的地方,我很抱歉,但是我希望你們都會幫助我理解我錯過了什麼。試圖從android日期選擇器返回日期

劇情簡介:我想用日期選取器填充幾個文本框,並且無法理解如何返回選定的日期,以便我可以填充相應的文本框。

基本上我試圖實現一個標準的日期選擇器針對android設備> = 4.0。

public static class DatePickerFragment extends DialogFragment 
         implements DatePickerDialog.OnDateSetListener { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current date as the default date in the picker 
     final Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day = c.get(Calendar.DAY_OF_MONTH); 

     // Create a new instance of DatePickerDialog and return it 
     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    public void onDateSet(DatePicker view, int year, int month, int day) { 
     // Do something with the date chosen by the user 
    } 
} 

public void showDatePickerDialog(View v) { 
    DialogFragment newFragment = new DatePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "datePicker"); 
} 

你會發現,這是從Android的文檔和示例代碼,可以在這裏找到:http://developer.android.com/guide/topics/ui/controls/pickers.html 我實現的一個大項目的代碼和日期選擇器自帶了罰款,日期被挑細等等。 我遇到麻煩的是在用戶選擇它之後得到日期。基本上用戶選擇日期然後什麼都不會發生。我想要做的是在選擇後填入具有日期的特定文本框。這將發生在幾個文本框中,所以我不能將它硬編碼到onDateSet函數中。 我的想法是將文本框的id與創建的datepicker一起傳遞,但我不完全確定我會如何做到這一點。

這是我第一個android項目,專注於理解,而不是簡單地得到一個工作項目,謝謝。

回答

0

當用戶選擇一個日期時,應該調用「onDateSet」方法。 在此方法中,您可以使用參數中傳遞的年份,月份和日期。

+0

onDateSet在用戶選擇日期時觸發。我不知道如何有足夠的上下文知道哪個對話框更新時,該功能被觸發。問題是我的活動有多個地方輸入日期,所以每次觸發我需要更新一個不同的文本框。我不知道如何在日期最終被選中時通過文本框更新到函數。 –

+0

噢好吧,所以我覺得不要實現OnDateSetListener,而應該在創建DatePickerDialog時創建一個新的OnDateSetListener。這樣,你將有多個「onDateSet」方法。 –

0

我不知道,如果是做的最好的方式,但我解決這個問題是這樣的:

  • 我有兩個按鈕,A和B.
  • 當我點擊按鈕A,出現DatePicker並且用戶選擇日期。
  • 確認日期後,按鈕A中的文本被設置爲選定的日期。
  • 爲按鈕B.

相同的行爲當按下按鈕A或B是函數:

public void selectDateButtons(View view){ 


    if(view.getId() == R.id.buttonA) 
    { 
      clickedButton = R.id.buttonA; 
    }else{ 
      clickedButton = R.id.buttonB; 
    } 

    DialogFragment newFragment = new DatePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "datePicker"); 


} 

clickedButton是靜態的全局變量。

然後聲明子類

public static class DatePickerFragment extends DialogFragment 
implements DatePickerDialog.OnDateSetListener { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the current date as the default date in the picker 
     final Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day = c.get(Calendar.DAY_OF_MONTH); 
     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int month, int day) { 

     if(clickedButton == R.id.ButtonA) 
     { 
      ButtonA.setText(String.valueOf(day) + "/" 
       + String.valueOf(month + 1) + "/" + String.valueOf(year)); 

     } else { 
      ButtonB.setText(String.valueOf(day) + "/" 
        + String.valueOf(month + 1) + "/" + String.valueOf(year)); 
     } 
    } 
} 

不要忘記,你的主要活動應該延伸FragmentActivity。 希望也適合你! 此致敬禮!

相關問題