Android日期/时间选择器(工具类)

来源:互联网 发布:网络宣传部 编辑:程序博客网 时间:2024/06/08 13:18
/** * 日期用法:DataPickDialogUtil dataPickDialogUtil = new DataPickDialogUtil(当前类.this); dataPickDialogUtil.dateTimePicKDialog(textview); 时间用法:DataPickDialogUtil timePickDialogUtil = new DataPickDialogUtil(当前类.this); timePickDialogUtil.TimePicKDialog(textview); * 日期/时间选择器 * Created by zyz on 2015/9/29. */public class DataPickDialogUtil{    private DatePicker datePicker;    private TimePicker timePicker;    private Activity activity;    private AlertDialog ad;    /**     * 日期时间弹出选择框构造函数     *     * @param activity     *            :调用的父activity     * @param     *     */    public DataPickDialogUtil(Activity activity) {        this.activity = activity;    }    /**     * 弹出日期时间选择框方法     *     * @param inputDate     *            :为需要设置的日期时间文本编辑框     * @return     */    public AlertDialog dateTimePicKDialog(final TextView inputDate) {        LinearLayout dateTimeLayout = (LinearLayout) activity                .getLayoutInflater().inflate(R.layout.common_datetime, null);        datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);        ad = new AlertDialog.Builder(activity)                .setTitle("请设置时间")                .setView(dateTimeLayout)                .setPositiveButton("设置", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int whichButton) {                        StringBuffer sb = new StringBuffer();                        sb.append(String.format("%d-%02d-%02d",                                datePicker.getYear(),                                datePicker.getMonth() + 1,                                datePicker.getDayOfMonth()));                        inputDate.setText(sb.toString());                        dialog.cancel();                    }                })                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int whichButton) {//                        inputDate.setText("");                    }                }).show();        return ad;    }    public AlertDialog TimePicKDialog(final TextView inputDate) {        LinearLayout dateTimeLayout = (LinearLayout) activity                .getLayoutInflater().inflate(R.layout.common_datetime, null);        datePicker = (DatePicker) dateTimeLayout.findViewById(R.id.datepicker);        timePicker = (TimePicker) dateTimeLayout.findViewById(R.id.timepicker);        datePicker.setVisibility(View.GONE);        timePicker.setVisibility(View.VISIBLE);        //是否使用24小时制        timePicker.setIs24HourView(true);        ad = new AlertDialog.Builder(activity)                .setTitle("请设置时间")                .setView(dateTimeLayout)                .setPositiveButton("设置", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int whichButton) {                        inputDate.setText(timePicker.getCurrentHour()+":"+timePicker.getCurrentMinute());                        dialog.cancel();                    }                })                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int whichButton) {//                        inputDate.setText("");                    }                }).show();        return ad;    }}


0 0