时间控件的使用

1、首先在oncreate方法里边初始化时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mQueue = Volley.newRequestQueue(mContext);
        setDateTime();
    }

2、逻辑代码:

“`
/**
* 设置日期
*/
private void setDateTime() {
final Calendar c = Calendar.getInstance();

    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
}

“`/**
* 更新日期显示
*/
private void updateDateDisplay() {
if (time != null) {
time.setText(new StringBuilder()
.append(mYear)
.append(“-“)
.append((mMonth + 1) < 10 ? “0” + (mMonth + 1)
: (mMonth + 1)).append(“-“).append(mDay));
}
}

/**
 * 日期控件的事件
 */
class PickerOnDateSetListener implements DatePickerDialog.OnDateSetListener {

    @Override
    public void onDateSet(DatePicker arg0, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;

        updateDateDisplay();
    }

}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, new PickerOnDateSetListener(),
                mYear, mMonth, mDay);
    }
    return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
    case DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
        break;
    }
}
@Override
public void onClick(View v) {
    super.onClick(v);
    switch (v.getId()) {
    case R.id.et_time:
        showDialog(DATE_DIALOG_ID);
        break;
}

你可能感兴趣的:(时间控件的使用)