首页 > 解决方案 > Android DatePickerDialog 不适用于 Android 5

问题描述

我正在开发一个应用程序,在注册表单上,用户需要输入他的生日。我使用了 DatePickerDialog。该应用程序在我的手机和模拟器上运行良好,但是当我在旧手机(android 5)上测试它时,当我想选择日期时,应用程序停止运行。

这是我的 XML 代码

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Date"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        app:boxStrokeColor="@color/colorPrimary"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="false"
            android:onClick="BirthDatePicker"
            />
    </com.google.android.material.textfield.TextInputLayout>

这是我的java代码

public void BirthDatePicker(View v) throws ParseException {
  final DatePickerDialog[] picker = new DatePickerDialog[1];
    final TextInputEditText eText;
    eText= findViewById(R.id.editText1);
    //eText.setInputType(InputType.TYPE_NULL);
    final Calendar cldr = Calendar.getInstance();
    int day = cldr.get(Calendar.DAY_OF_MONTH);
    int month = cldr.get(Calendar.MONTH);
    int year = cldr.get(Calendar.YEAR);
    // date picker dialog  DatePickerDialog.OnDateSetListener listener
    picker[0] = new DatePickerDialog(RegisterActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                eText.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                setDate( dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
                }
            }, year, month, day);

    picker[0].show();
}

二传手:

 public void setDate(String date) {
        mDate = date;
    }

标签: androiddatepickerdatepickerdialog

解决方案


由于此限制,我将我的评论放在答案字段中:“您必须有 50 名声望才能发表评论”。

粘贴实际设备的 logcat 详细信息。

我认为这与“风格”问题有关。

请检查android 5 lolipop 中的日期选择器对话框问题


推荐阅读