如何从各种设备中的日期选择器中获取所选日期?

时间:2013-11-28 10:42:00

标签: android datepicker

我正在显示我的时间选择器,如下所示,

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只做了选项。

我的问题是,有没有其他方法来获取所选日期,为什么会发生?

我是在一个片段中这样做的。

提前致谢

2 个答案:

答案 0 :(得分: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...  

答案 1 :(得分: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);
    }