【问题标题】:Android DatePickerDialog Api 23 (Android 6)Android DatePickerDialog Api 23 (Android 6)
【发布时间】:2016-06-25 04:08:54
【问题描述】:

我正在触发一个 DatePickerDialog,它在 api 22(Android 5.1)之前工作并显示良好,我正在设置混合日期和最大日期(最小值 = 当前日期,最大值 = 从当前日期开始的 1 个月),但它只是在 Api 23 中显示当前日期,我附上了代码和图像。

///////////////////////////////datepickerdialog///////////////////////////////
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        final Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        /*
            Create a DatePickerDialog using Theme.

                DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
                    int year, int monthOfYear, int dayOfMonth)
         */

        // DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
        DatePickerDialog dpd = new DatePickerDialog(getActivity(),this,year,month,day);

        if (Build.VERSION.SDK_INT < 22){

            dpd.getDatePicker().setCalendarViewShown(true);
            dpd.getDatePicker().setSpinnersShown(false);
            dpd.getDatePicker().getCalendarView().setShowWeekNumber(false);

        }

        calendar.setTimeInMillis(calendar.getTimeInMillis());
        long mindate = calendar.getTime().getTime();
        calendar.add(Calendar.MONTH, 1);
        long maxdate = calendar.getTime().getTime();

        dpd.getDatePicker().setMinDate(mindate);
        dpd.getDatePicker().setMaxDate(maxdate);

        dpd.getDatePicker().setMinDate(mindate);

        if(Build.VERSION.SDK_INT < 23){

            dpd.setTitle("");

        }

        // Return the DatePickerDialog
        return  dpd;
    }

    @SuppressLint("SimpleDateFormat")
    public void onDateSet(DatePicker view, int year, int month, int day){
        // Do something with the chosen date

        month++;

        evento_fecha = year+"-"+month+"-"+day;          


        month--;

        datetime.set(Calendar.YEAR, year);
        datetime.set(Calendar.MONTH, month);
        datetime.set(Calendar.DAY_OF_MONTH, day);

        SimpleDateFormat mSDF = new SimpleDateFormat("dd/MM/yyyy");
        fecha = mSDF.format(datetime.getTime());

        //fecha_seleccionada.setText(dia+"/"+mes+"/"+year);
        fecha_seleccionada.setText(fecha);

    }
} 

答案 1:结果

【问题讨论】:

  • 好的,我正在三星 Android 4.1 上进行测试,但出现错误,请参见上图(问题中)
  • 你用的是什么代码??
  • 错误在这行:long mindate = System.currentTimeMillis(); dpd.getDatePicker().setMinDate(mindate);
  • 我正在使用您的线路来设置最小和最大日期
  • 查看我的更新答案。

标签: android android-6.0-marshmallow android-5.1.1-lollipop


【解决方案1】:

DatePicker / DatePickerDialog 在 Android Marshmallow / API 23 上未正确显示是主题问题。我建议不要使用默认主题或编写自定义样式,而是使用 'theme' 选项实例化 DatePickerDialog:

DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth)

从 API 23 开始,对话框应该使用 Material 主题。主题参数使用android.R.style.Theme_Material_Light_Dialog_Alert。

对于 Xamarin Android - 使用 Android.Resource.Style.ThemeMaterialLightDialogAlert。

【讨论】:

    【解决方案2】:

    我在我的主题中使用了这种风格:

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="colorPrimary">@color/green_sheet</item>
        <item name="android:textColorPrimary">@color/textColorPrimary</item>
        <item name="android:textAllCaps">false</item>     
    
    </style> 
    

    textColorPrimary 属性以白色显示所有天数,我使用此属性将操作栏文本设置为白色。我错了,因为我必须为此目的使用 Theme.AppCompat.Light.DarkActionBar,所以我将主题样式更改为:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="colorPrimary">@color/green_sheet</item>
    
        <item name="android:textAllCaps">false</item>     
    
    </style>
    

    结果符合预期。

    【讨论】:

      【解决方案3】:

      在您的DatePicker 中,您已将MinDate 设置为currentTimeMillis(),在您的MaxDate 中,您已将1 MONTH 添加到您的Calendar。

      只需简单地使用此代码。

      public class MainActivity extends Activity {
      
          int mYear, mMonth, mDay;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
      
              final Calendar c = Calendar.getInstance();
              mYear = c.get(Calendar.YEAR);
              mMonth = c.get(Calendar.MONTH);
              mDay = c.get(Calendar.DAY_OF_MONTH);
      
              final TextView textView = (TextView)findViewById(R.id.textview_totalStone);
              textView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
      
                      DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
                              new DatePickerDialog.OnDateSetListener() {
                                  @Override
                                  public void onDateSet(DatePicker view, int year, int month, int day) {
                                      c.set(year, month, day);
                                      String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime());
                                      textView.setText(date);
      
                                      mYear = c.get(Calendar.YEAR);
                                      mMonth = c.get(Calendar.MONTH);
                                      mDay = c.get(Calendar.DAY_OF_MONTH);
                                  }
                              }, mYear, mMonth, mDay);
                      dpd.getDatePicker().setMinDate(System.currentTimeMillis());
      
                      Calendar d = Calendar.getInstance();
                      d.add(Calendar.MONTH,1);
      
                      dpd.getDatePicker().setMaxDate(d.getTimeInMillis());
                      dpd.show();
      
      
                  }
      
              });
      
          }
      

      在我的 DatePicker 中,我有日期 June 25,它选择了你的要求,你有 MaxDate 1 个月,它显示在屏幕截图中。

      屏幕截图:1

      屏幕截图:2

      更新:

      替换你的这个代码

      calendar.setTimeInMillis(calendar.getTimeInMillis());
              long mindate = calendar.getTime().getTime();
      
              dpd.getDatePicker().setMinDate(mindate);
      

      有了这个

        dpd.getDatePicker().setMinDate(System.currentTimeMillis());
      

      【讨论】:

      • 我在 Android 模拟器上测试了我的代码(问题中的那个),它工作正常,日历看起来像你的,但在真实设备上。 (我上面附的图片属于摩托罗拉g3手机)
      • 这是一个主题问题,星期几显示白色而不是黑色(允许)和灰色(不允许)
      • 想象一下,在您的示例中,所有天数都是白色的,但当前日期
      • @user4216713 因为它被选中了Date,所以它的颜色是White。
      • 这是一个很好的答案,但我的代码没问题,就像我说的,这是一个与应用主题相关的问题(Api 23)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 2016-11-12
      • 2015-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多