2013-11-28 80 views
0

我顯示如下我的時間選擇器,如何從各種設備的dateppicker中獲取所選日期?

DatePickerDialog dateChange = new DatePickerDialog(getActivity(), DateSetListener, Year, Month,Day); 
    dateChange.show(); 

我獲得通過下面的代碼此值,

private DatePickerDialog.OnDateSetListener DateSetListener = 
     new DatePickerDialog.OnDateSetListener() 
     { 

      public void onDateSet(DatePicker view, int year, 
            int monthOfYear, int dayOfMonth) 
      { 
       Year = year; 
       Month = monthOfYear+1; 
       Day = dayOfMonth; 
       date = Day+"-"+ Month +"-"+Year; 
      } 
     }; 

通過這一點,我得到模擬器日期,

,但在設備(Sony Xperia C,Jellybean)我無法獲取選定的日期,並且datepicker顯示設置按鈕和取消按鈕,但在我的模擬器中,datepicker只做了選項。

我的問題是有沒有其他方式來獲得選定日期,爲什麼會發生?

我正在做一個片段。

在此先感謝

+0

檢查您在仿真器和設備上運行的API版本。 –

+0

我的api版本是18,在那個設備上它可能更小但不會太多 – user3017713

回答

3

試試這個,

public class DateAndTimePickerActivity extends Activity implements 
    OnClickListener { 

Button dateBtn, timeBtn; 
static final int DATE = 0; 
static final int TIME = 1; 
int year, month, day, hour, minute,am_pm; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_date_and_time_picker); 

    dateBtn = (Button) findViewById(R.id.date); 
    timeBtn = (Button) findViewById(R.id.time); 
    dateBtn.setOnClickListener(this); 
    timeBtn.setOnClickListener(this); 

    Calendar calendar = Calendar.getInstance(); 
    year = calendar.get(Calendar.YEAR); 
    month = calendar.get(Calendar.MONTH); 
    day = calendar.get(Calendar.DAY_OF_MONTH); 

    hour = calendar.get(Calendar.HOUR); 
    minute = calendar.get(Calendar.MINUTE); 
    am_pm=calendar.get(Calendar.AM_PM); 

} 

@SuppressWarnings("deprecation") 
@Override 
public void onClick(View v) { 

    switch (v.getId()) { 
    case R.id.time: 
     showDialog(TIME); 
     break; 
    case R.id.date: 
     showDialog(DATE); 
     break; 
    default: 
     break; 
    } 

} 

DatePickerDialog.OnDateSetListener dateListener = new OnDateSetListener() { 

    @Override 
    public void onDateSet(DatePicker view, int Currentyear, 
      int monthOfYear, int dayOfMonth) { 

     year = Currentyear; 
     month = monthOfYear; 
     day = dayOfMonth; 
     Toast.makeText(getApplicationContext(), 
       "Date:" + day + "/" + month + "/" + year, 1).show(); 
    } 
}; 

TimePickerDialog.OnTimeSetListener timeListener = new OnTimeSetListener() { 

    @Override 
    public void onTimeSet(TimePicker view, int hourOfDay, int minuteOfDay) { 
     hour = hourOfDay; 
     minute = minuteOfDay; 
     Toast.makeText(getApplicationContext(), 
       "Time::" + hour + ":" + minute, 1).show(); 
    } 
}; 

@Override 
@Deprecated 
protected Dialog onCreateDialog(int id) { 

    switch (id) { 
    case DATE: 

     return new DatePickerDialog(this, dateListener, 
       year, month, day); 

    case TIME: 
     return new TimePickerDialog(this, timeListener, 
       hour, minute, false); 

    } 
    return null; 
} 
} 


definitaly it will work... 
0

使用DateDialogFragment這樣的:

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); //get the fragment 
    DateDialogFragment frag = DateDialogFragment.newInstance(this, new DateDialogFragmentListener(){ 
      public void updateChangedDate(int year, int month, int day){ 
      displayDate = padding_str(day)+"-"+padding_str(month+1)+"-"+padding_str(year); 
      sendDate = padding_str(year)+"-"+padding_str(month+1)+"-"+padding_str(day); 
      } 
      }, now); 
      frag.show(ft, "DateDialogFragment"); 


private static String padding_str(int c) { 
     if (c >= 10) 
      return String.valueOf(c); 
     else 
      return "0" + String.valueOf(c); 
    } 
相關問題