【发布时间】:2020-03-25 19:59:38
【问题描述】:
我正在开发一个应用程序,在注册表单上,用户需要输入他的生日。我使用了 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;
}
【问题讨论】:
标签: android datepicker datepickerdialog