package com.deepblue.datapicker;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class DataPickerDialogActivity extends AppCompatActivity {
private TextView tvProcessName;//用来展示选中日期的tv
private Button mClickBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
tvProcessName = (TextView) findViewById(R.id.tvProcessName);
mClickBtn = (Button) findViewById(R.id.click);
/**
* 点击按钮显示DatePickerDialog
*/
mClickBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDataDialog();
}
});
}
/**
* 显示日期选择器
*/
private void showDataDialog() {
/**
* in a fullscreen DatePicker is the choice of parent theme:
* 主题(不设置的话就是默认的 ,xiaomi mi 3c 是6.0系统,显示的就是日历样式的)
* 设置方法是 AlertDialog.THEME_HOLO_LIGHT,
* 直接new就可以了 DatePickerDialog(Context context, int themeResId, DatePickerDialog.OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth)
** @deprecated *//*
@Deprecated
public static final int THEME_DEVICE_DEFAULT_DARK = 4;
*//** @deprecated *//*
@Deprecated
public static final int THEME_DEVICE_DEFAULT_LIGHT = 5;
*//** @deprecated *//*
@Deprecated
public static final int THEME_HOLO_DARK = 2;
*//** @deprecated *//*
@Deprecated
public static final int THEME_HOLO_LIGHT = 3;
*//** @deprecated *//*
@Deprecated
public static final int THEME_TRADITIONAL = 1;
*/
Calendar calendar = Calendar.getInstance();
DatePickerDialog datePickerDialog = new DatePickerDialog(DataPickerDialogActivity.this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// TODO Auto-generated method stub
int mYear = year;
int mMonth = month;
int mDay = day;
//更新EditText控件日期 小于10加0
tvProcessName.setText(new StringBuilder()
.append(mYear)
.append("-")
.append((mMonth + 1) < 10 ? 0 + (mMonth + 1) : (mMonth + 1))
.append("-")
.append((mDay < 10) ? 0 + mDay : mDay));
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
//设置时间范围
// dateDialog.getDatePicker().setMinDate(CalendarUtil.GetLastMonthDate().getTimeInMillis());
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
datePickerDialog.show();
}
}