Android版的DatePicker没有星期天 [英] Android DatePicker without sundays

查看:263
本文介绍了Android版的DatePicker没有星期天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是日期选择器选择一个特定的日期。在日期选择器不应该允许挑周日。截至现在我选择检测后,周日和显示信息,但它会更漂亮,如果日期选择器不允许周日。有没有办法做到这一点?

code检测星期日:

 私有静态类DatePickerFragment扩展DialogFragment实现DatePickerDialog.OnDateSetListener
 {    @覆盖
    公共对话框onCreateDialog(捆绑savedInstanceState)
    {
     [...]
    }    公共无效onDateSet(查看的DatePicker,年整型,诠释月,日整型)
    {
        现在日历= Calendar.getInstance();
        日历choosen = Calendar.getInstance();
        choosen.set(年,月,日);
        如果(now.compareTo(choosen)℃,)
        {
            UserMessage.showMessage(已选定的日期是在未来,无效的日期,getActivity());
            返回;
        }否则如果(choosen.get(Calendar.DAY_OF_WEEK)== Calendar.SUNDAY)
        {
            UserMessage.showMessage(已选定的日期是星期天!,无效的日期,
                    getActivity());
            返回;
        }
        [...]
    }
}


解决方案

我解决了这个要求与创建含日期选择器自定义XML布局的自定义对话框。

我创建为大家下面的例子中,谁需要一个datepicker对话有特殊需求的一样,没有星期天,没有日期之前要提供一个具体日期来选择:

https://www.open-sourced.de/show_article.php?条款ArticleID = 56

I am using an datepicker to choose a specific date. The datepicker should not allow to pick sundays. Till now i am detecting sundays after choice and show a message but it would be more nice if the datepicker doesn't allow sundays. Is there a way to do this?

Code to detect sundays:

 private static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
 {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
     [...]
    }

    public void onDateSet(DatePicker view, int year, int month, int day)
    {
        Calendar now = Calendar.getInstance();
        Calendar choosen = Calendar.getInstance();
        choosen.set(year, month, day);
        if (now.compareTo(choosen) < 0)
        {
            UserMessage.showMessage("The choosen date is in future", "Invalid date", getActivity());
            return;
        } else if (choosen.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
        {
            UserMessage.showMessage("The choosen date is on sunday!", "Invalid date",
                    getActivity());
            return;
        }
        [...]
    }
}         

解决方案

I solved this requirements with creating a custom dialog containing a custom xml layout with datepicker.

I created the following example for everyone, who needs a datepicker dialog with special needs like no sundays, no dates earlier a specific date to be offered to choose:

https://www.open-sourced.de/show_article.php?articleid=56

这篇关于Android版的DatePicker没有星期天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆