2014-07-20 116 views
0

我檢查了多個線程,但沒有人似乎回答我的問題。從Android開發網站,我創建了一個日期選取片段:Android從datepicker獲取日期

public class DatePickerFragment extends DialogFragment implements 
DatePickerDialog.OnDateSetListener{ 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState){ 
     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); 
    } 

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

    } 
} 

在我的活動我只要致電:

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

如何傳遞的日期從選擇器回活動的用戶挑選?基本上我只想一個文本字段設置爲日期:

mTextField.set(*The date*); 

回答

1

我會建議把你在你的activityDatePickerFragment內部類,當用戶完成填寫日期就可以使用onDateSet設置您的mTextField的文字。

public void onDateSet(DatePicker view, int year, int month, int day) { 
    mTextField.set(*The date*); //make sure that DatePickerFragment is an inner class 
} 
0

試試這個:

pickDate = (Button) findViewById(R.id.pick_date_new); 
    pickDate.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      DialogFragment datePickerFragment = new DialogFragment() { 
       DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() { 
        @Override 
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { 
         dateLabel.setText(i2 + "/" + (i1 + 1) + "/" + i); 
        } 
       }; 

       @Override 
       public Dialog onCreateDialog(Bundle savedInstanceState) { 
        return new DatePickerDialog(getActivity(), listener, 1970, 0, 1); 
       } 

      }; 
      datePickerFragment.show(getFragmentManager(), "datePicker"); 
     } 
    });