草庐IT

java - setMaxDate() 在 Lollipop 5.0.1 android 上无法完美运行,需要适当的解决方案

coder 2023-09-02 原文

即使在将 setMaxDate() 应用于日期选择器之后,我仍然能够在 lollipop 5.0.1 上选择禁用的日期。该代码适用于除 lollipop 5.0.1 以外的所有其他版本的 android。

在通过设置 setMaxDate() 限制日期后,任何用户都不能选择禁用的日期。如何以编程方式为 DatePicker 实现此目的?

我试过下面的代码:-

datePickerDialog = new DatePickerDialog(myContext, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        }
    }, mYear, mMonth, mDay);
    datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
    datePickerDialog.setCanceledOnTouchOutside(true);
    datePickerDialog.setCancelable(true);
    datePickerDialog.show();

我也尝试了以下解决方案,但这些似乎不起作用:-

datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());

Calendar maxCal = Calendar.getInstance();
datePickerDialog.getDatePicker().setMaxDate(maxCal.getTimeInMillis());

Date maxDate = new Date();
datePickerDialog.getDatePicker().setMaxDate(maxDate.getTime());

请向 setMaxDate() 提供适用于 lollipop 的解决方案。

或者,如果您有任何其他答案,请尝试包括官方报价和资源或链接(如 android 开发者网站),如果您知道并提供简短的详细信息。提前致谢。

最佳答案

正如评论中已经指出的那样,这个有问题的日历没有真正完美的解决方案(在 Android 5.0 上你可以选择一个无效的日期,在一些三星设备上出现了一个奇怪的行为:按下后退按钮实际上是在选择一个日期和其他人我真的不记得了)。但是,当您可以通过检查其有效性并在失败时显示 Toast 来选择无效日期时,我解决了这个问题。这不会阻止用户实际选择无效日期,而是强制用户选择有效日期以继续使用该应用。

   DatePickerDialog dialog = new DatePickerDialog(getActivity(), mOnDateSetListener, year, month, day) {
        private boolean allowClose = false;

        @Override
        public void onBackPressed() {
            allowClose = true;
            super.onBackPressed();
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            allowClose = true;
            if (which == DialogInterface.BUTTON_POSITIVE && !validate()) {
                allowClose = false;
                Toast.makeText(getContext(), R.string.invalid_date, Toast.LENGTH_SHORT).show();
            }
            if (allowClose) {
                super.onClick(dialog, which);
            }
        }

        @Override
        public void dismiss() {
            if (allowClose) {
                super.dismiss();
            }
        }
    };

通过使用它,您可以确保如果用户选择了一个无效日期,您将给他一个正确的警告消息并在您按下 Ok 按钮时阻止日历关闭。应该创建 validate 函数来满足您的需求,但我认为我所拥有的就可以了:

private boolean validate() {
    if (this.getDialog() instanceof DatePickerDialog) {
        DatePicker datePicker = ((DatePickerDialog) this.getDialog()).getDatePicker();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, datePicker.getYear());
        calendar.set(Calendar.MONTH, datePicker.getMonth());
        calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        boolean valid = false;
        Calendar minCalendar = Calendar.getInstance();
        minCalendar.setTimeInMillis(datePicker.getMinDate());
        Calendar maxCalendar = Calendar.getInstance();
        maxCalendar.setTimeInMillis(datePicker.getMaxDate());
        if (DateUtils.in(calendar.getTime(), minCalendar.getTime(), maxCalendar.getTime())) {
            valid = true;
        }
        return valid;
    }
    return false;
}

最后,我不保证这是最好的解决方案,但我已经使用过它并且没有收到用户的任何投诉。

关于java - setMaxDate() 在 Lollipop 5.0.1 android 上无法完美运行,需要适当的解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43908314/

有关java - setMaxDate() 在 Lollipop 5.0.1 android 上无法完美运行,需要适当的解决方案的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  4. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  5. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  6. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  7. ruby - rspec 需要 .rspec 文件中的 spec_helper - 2

    我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只

  8. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  9. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  10. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

随机推荐