gpt4 book ai didi

android - 在 Android 5.0 Lollipop 上不支持 calendarViewShown

转载 作者:IT老高 更新时间:2023-10-28 23:30:24 26 4
gpt4 key购买 nike

我正在阅读 The Big Nerd Ranch 的 Android 指南。在里面分配犯罪 Intent 我正在使用 DatePicker 与以下布局。

在 5.0 之前的设备上,日历不显示在 DatePicker 中,如布局,但 Android 5.0 忽略了这一点并显示了一个日历。任何想法这里可能发生了什么?

完整代码可在 Github 上获得.

dialog_date.xml

<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_date_datePicker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false" >
</DatePicker>

DatePickerFragment.java

package com.sudhirkhanger.android.criminalintent;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;

public class DatePickerFragment extends DialogFragment {

public static final String EXTRA_DATE = "com.sudhirkhanger.android.criminalintent.date";
private Date mDate;

public static DatePickerFragment newInstance(Date date) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_DATE, date);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
}

private void sendResult(int resultCode) {
if (getTargetFragment() == null)
return;
Intent i = new Intent();
i.putExtra(EXTRA_DATE, mDate);
getTargetFragment().onActivityResult(getTargetRequestCode(),
resultCode, i);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mDate = (Date) getArguments().getSerializable(EXTRA_DATE);
// Create a Calendar to get the year, month, and day
final Calendar calendar = Calendar.getInstance();
// calendar.setTime(mDate);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);

View v = getActivity().getLayoutInflater().inflate(
R.layout.dialog_date, null);

DatePicker datePicker = (DatePicker) v
.findViewById(R.id.dialog_date_datePicker);
datePicker.init(year, month, day, new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int month,
int day) {
// Translate year, month, day into a Date object using a
// calendar
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
mDate = new GregorianCalendar(year, month, day, hour, minute)
.getTime();
// Update argument to preserve selected value on rotation
getArguments().putSerializable(EXTRA_DATE, mDate);
}
});

return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
sendResult(Activity.RESULT_OK);
}
}).create();

}
}

最佳答案

解决方案已发布here

您必须同时使用 android:calendarViewShown="false"android:datePickerMode="spinner"

这样就解决了。

<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_date_datePicker"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:calendarViewShown="false"
android:datePickerMode="spinner" >
</DatePicker>

关于android - 在 Android 5.0 Lollipop 上不支持 calendarViewShown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27212267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com