gpt4 book ai didi

android-dialog - ShowDIalog() 中的 DatePickerDialog 已弃用

转载 作者:行者123 更新时间:2023-12-03 22:41:13 24 4
gpt4 key购买 nike

我有下一个代码:

dateDisplay1.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
option = 1;
showDialog(DATE_DIALOG_ID);
}
});

还有 onCreateDialog 函数:

protected Dialog onCreateDialog(int id) {

Calendar cal = Calendar.getInstance();

switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dayDate, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
case TIME_DIALOG_ID:
return new TimePickerDialog(this, timeDate, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false);
}
return null;
}

我想做的是将已弃用的 showDialog() 改编为 DialogFragment,但没有成功。下面的代码有效,但不是最佳实践。所以我想更正代码。

您将如何实现?

最佳答案

public class ReminderEditActivity extends Activity {
private Button mDateButton;
private Button mTimeButton;
public Calendar mCalendar;
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String TIME_FORMAT = "kk:mm";
DialogFragment dateFragment;
DialogFragment timeFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_edit);
mTimeButton = (Button) findViewById(R.id.reminder_time);
mDateButton = (Button) findViewById(R.id.reminder_date);
mCalendar = Calendar.getInstance();
mDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog(v);
}
});
mTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showTimePickerDialog(v);
}
});

}

public void showDatePickerDialog(View v) {
dateFragment = new DatePickerFragment();
dateFragment.show(getFragmentManager(), "datePicker");

}

public void showTimePickerDialog(View v) {
timeFragment = new TimePickerFragment();
timeFragment.show(getFragmentManager(), "timePicker");
}

public void updateDateButtonText() {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
String dateForButton = dateFormat.format(mCalendar.getTime());
mDateButton.setText(dateForButton);
}
private void updateTimeButtonText() {
SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
String timeForButton = timeFormat.format(mCalendar.getTime());
mTimeButton.setText(timeForButton);
}

class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DAY_OF_MONTH, day);
updateDateButtonText();
}
}

public class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}

public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);
updateTimeButtonText();
}
}

}

关于android-dialog - ShowDIalog() 中的 DatePickerDialog 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18838052/

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