2013-02-02 82 views
0

我有下面的代碼添加一個選項按鈕:如何DialogFragment

public void startDatePickerDialog(View v) { 
    DatePickerFragment newFragment = new DatePickerFragment(); 
    newFragment.setType("start"); 
    newFragment.show(getSupportFragmentManager(), "startDatePicker"); 
} 

與DatePickerFragment

private String type = "none"; 

@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); 
} 

@Override 
public void onCancel(DialogInterface dialog) { 
    Toast.makeText(getActivity(), "works?", Toast.LENGTH_LONG).show(); 
    if (type.equals("start")) { 
     EditText e = (EditText) getActivity().findViewById(
       R.id.date_of_task); 
     e.setText(""); 
    } else { 
     EditText e = (EditText) getActivity().findViewById(
       R.id.deadlineDate); 
     e.setText(""); 
    } 
    super.onCancel(dialog); 
} 

public void setType(String t) { 
    type = t; 
} 

@Override 
public void onDateSet(DatePicker view, int year, int month, int day) { 
    if (type.equals("start")) { 
     EditText e = (EditText) getActivity().findViewById(
       R.id.date_of_task); 
     e.setText("" + day + "." + (month + 1) + "." + year); 
    } else { 
     EditText e = (EditText) getActivity().findViewById(
       R.id.deadlineDate); 
     e.setText("" + day + "." + (month + 1) + "." + year); 
    } 
} 

我現在的問題是,onCancel不起作用。因此,我想在DialogFragment上添加一個Button,這會導致onCreate中的操作。我如何添加按鈕或如何解決這個問題。

回答

0

日期/時間pickerdialog存在一個錯誤。它是這樣的:

現在: 點擊OK - 日期被設置兩次 點擊取消 - 日起被清除,然後設置 單擊後退 - 日期被設定一次

預期: 點擊OK - 日期被設置一次 點擊取消 - 日期越來越重 點擊回 - ntng和它保留,因爲它是

作爲國家工作周圍,你可以保留一個計數器,將獲得首次設置調用OnDateSetListener如果計數器> 1,則返回並重置計數器。

0

好吧我解決了我的問題,設置DatePickerDialog的方法「setButton2」。我生成了一個OnclickListener,並將我的代碼放入onClick方法中,並且它可以工作