2013-04-22 73 views
1

我在我的活動中使用DatePicker對話框以供用戶選擇日期。 我想要做的是在textView中顯示選定的日期。任何人都可以請幫助我如何獲得選定的日期,然後相應地顯示它。 在此先感謝!Android - 日期選擇器 - Obatin值

以下是我使用來實現的DatePicker對話框代碼:

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 yyyy, int mm, int dd) { 
      // Do something with the date chosen by the user 


      } 
} 

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

回答

1
在onDateSet方法

String cuurent_date = String.valueOf(c.get(Calendar.DAY_OF_MONTH)) 
      + "/" + String.valueOf(c.get(Calendar.MONTH) + 1) + "/" 
      + String.valueOf(c.get(Calendar.YEAR)); 
+0

將Textview聲明爲public&static並在dateSet方法中調用它by-className.textviewName.setText(); – user2251725 2013-04-22 07:13:24

+0

@RaghavKumar是你的問題解決? – user2251725 2013-04-22 09:50:23

+0

這樣做,它會給出一個錯誤 - 「無法對類型爲」「的非靜態方法findViewById(int)進行靜態引用」 這就是我如何聲明文本視圖: public static TextView showSelDate =(TextView )findViewById(R.id.textView3); – 2013-04-22 11:05:28

相關問題