2014-02-13 74 views
1

我正在尋找在論壇和網絡,但我找不到解決我的問題。 我正在使用DatePicker和Timepicker的程序中工作。在我的程序中,我使用Google的默認定義(http://developer.android.com/guide/topics/ui/controls/pickers.html),爲每個選擇器(時間和日期)分隔類。DatePicker和TimePicker API <11

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

} 

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

這是DatePickerFragment類:

public class DatePickerFragment extends DialogFragment { 

@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(), 
      (OnDateSetListener) getActivity(), year, month, day); 
} 
    } 

我看了一些關於支持庫和我的活動改爲FragmentActivity,但我得到了一些錯誤。 我應該如何更改我的代碼以在低於3.0的版本中實現? 提前謝謝!

+1

讓你從android.support.v4進口 – pedromss

+0

我做了這件事,我改變了擴展活動的FragmentActivity和getFragmentManager()getSupportFragmentManager(),但有錯誤,在最後的變化:S – RaZieL

+0

最後,它的工作!感謝您的意見,請致電 – RaZieL

回答

0

解決方案:這裏 的問題是,我用android.app.DialogFragment,而不是android.support.v4 .app.Dialog.Fragment。 所以,我進口android.support.v4庫,以我的三個班(MainActivity,DatePickerFragment和TimePickerFragment。

下一步是改變擴展我的MainActivity類別的活動FragmentActivity(改變進口語句):

public class MainActivity extends FragmentActivity implements 
     DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener {} 

最後,我在這兩個CLA改變getFragmentManager()getSupportFragmentManager()分類:

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

public void showTimePickerDialog(View v) { 
    DialogFragment newFragment = new TimePickerFragment(); 
    newFragment.show(getSupportFragmentManager(), "timePicker"); 
} 

最後一步是在android 2.3.3模擬器上進行測試,它工作正常。