2012-08-01 51 views
14

我正在按照這裏的教程: http://developer.android.com/guide/topics/ui/controls/pickers.htmlAndroid Datepicker Fragment - 用戶設置日期時如何做某事?

我得到的東西工作,日期選擇器顯示。但是,我堅持如何在用戶設置日期時採取行動。

我看到他們有這樣的方法:

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

但是,什麼也不做,如果我把所有的代碼在裏面。

我對Java並不熟悉,所以我不確定需要額外的代碼才能更新所選日期的文本視圖。

我沒有添加以下代碼:

private DatePickerDialog.OnDateSetListener mDateListener = 
     new DatePickerDialog.OnDateSetListener() { 
      public void onDateSet(DatePicker view, int year, int month, int day) { 
       updateDate(year, month, day); 
      } 
     }; 

public void updateDate(int year, int month, int day) { 
    TextView bdate = (TextView)findViewById(R.id.birthDate); 
    Date chosenDate = new Date(year, month, day); 
    java.text.DateFormat dateFormat = DateFormat.getDateFormat(getApplicationContext()); 
    bdate.setText(dateFormat.format(chosenDate)); 
} 

但是,什麼都不做。

我也覺得這幾乎確切的問題: Get date from datepicker using dialogfragment

然而,在這種解決辦法將不會使用(EditSessionActivity)限制DatePickerFragment類只與EditSessionActivity工作?

回答

9

您應該使用靜態工廠將參數傳遞給DialogFragment。從docs,

「每個片段必須有一個空的構造函數,因此它可以在恢復其活動狀態時實例化。強烈建議子類沒有帶參數的其他構造函數,因爲這些構造函數在調用時不會被調用該片段被重新實例化;相反,調用者可以使用setArguments(Bundle)提供參數,然後由片段用getArguments()檢索。

類似地,Fragment子類應該是靜態的,以便它可以在不依賴父活動或Fragment的情況下重新實例化自身。如果您不遵守這些準則,您的片段在嘗試恢復時有時會發生崩潰或錯誤。

從Android的docs建立DialogFragment的推薦方法是使用靜態工廠方法。下面是需要在初始日期和OnDateChangedListener日期選取片段的例子:

public static class DatePickerFragment extends DialogFragment{ 

    private OnDateSetListener onDateSetListener; 

    static DatePickerFragment newInstance(Date date, OnDateSetListener onDateSetListener) { 
     DatePickerFragment pickerFragment = new DatePickerFragment(); 
     pickerFragment.setOnDateSetListener(onDateSetListener); 

     //Pass the date in a bundle. 
     Bundle bundle = new Bundle(); 
     bundle.putSerializable(MOVE_IN_DATE_KEY, date); 
     pickerFragment.setArguments(bundle); 
     return pickerFragment; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     super.onCreateDialog(savedInstanceState); 

     Date initialDate = (Date) getArguments().getSerializable(MOVE_IN_DATE_KEY); 
     int[] yearMonthDay = ymdTripleFor(initialDate); 
     DatePickerDialog dialog = new DatePickerDialog(getActivity(), onDateSetListener, yearMonthDay[0], yearMonthDay[1], 
       yearMonthDay[2]); 
     return dialog; 
    } 

    private void setOnDateSetListener(OnDateSetListener listener) { 
     this.onDateSetListener = listener; 
    } 

    private int[] ymdTripleFor(Date date) { 
     Calendar cal = Calendar.getInstance(Locale.US); 
     cal.setTime(date); 
     return new int[]{cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)}; 
    } 
} 

然後,您可以創建這些對話的片段之一,如下所示:

DatePickerFragment.newInstance(yourDate, yourOnDateSetListener); 
+1

也許你應該補充一點的對話框彈出,如果你先初始化片段,然後用它的「秀( )'使它出現在屏幕上的方法。 – 2015-01-15 10:51:27

2

我相信一個可以使用DialogFragments這裏:

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

定義DatePickerFragment並實現下面的方法

public void onDateSet(DatePicker view, int year, int month, int day) { 
// Do something with the date chosen by the user 
DateEdit.setText(day + "/" + (month + 1) + "/" + year); 
} 

取自Android DialogFragment Example

1

在我的經驗這個代碼工作完美!,我使用win10 Android Studio 2.2 ...

import android.app.Dialog; 
import android.support.v4.app.DialogFragment; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.app.DatePickerDialog; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import java.util.Calendar; 


public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);} 

    public void datePicker(View view){ 
     DatePickerFragment fragment_date = new DatePickerFragment(); 
     fragment_date.show(getSupportFragmentManager(),"datePicker");} 

    @Override 
    public void onDateSet(DatePicker view, int year, int month, int day) { 
     String day_x = ""+ day ,month_x = ""+(month+1); 
     if (day < 9){ day_x = "0" + day_x;} 
     if (month < 9){ month_x = "0" + month_x;} 
     ((EditText) findViewById(R.id.txtDate)).setText(day_x + "/" + month_x + "/" + year);} 

    public static class DatePickerFragment extends DialogFragment { 
     @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() ,(DatePickerDialog.OnDateSetListener)this,day,month,year);}} 

} 

我有一個「TextView的txtDate」和一個「按鈕上用onclick =日期選擇器」

相關問題