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

/**
 * 日期用法: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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值