我有一个警报系统,我希望用户设置警报通知的时间。我一直在尝试将timepicker对话框的值转换为文本视图,但无法正常工作。时间选择器也处于碎片状态,所以也许这就是它无法正常工作的原因。
public class fragment1_myreminders extends Fragment implements TimePickerDialog.OnTimeSetListener {
private static final String TAG = "fragment1_myreminders";
private Button mtimePickerButton;
private TextView mTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_myreminders,container,false);
mtimePickerButton = view.findViewById(R.id.timePickerButton);
mTextView = view.findViewById(R.id.setTime);
mtimePickerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment timepicker = new dialog_timePicker();
timepicker.show(getActivity().getSupportFragmentManager(), "TIME PICKER FRAG");
}
});
return view;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
calendar.set(Calendar.SECOND,0);
updateTimeText(calendar);
startAlarm(calendar);
}
private void updateTimeText(Calendar calendar) {
String timeText = "Alarm set for : ";
timeText+= DateFormat.getTimeInstance(DateFormat.SHORT).format(calendar.getTime());
mTextView.setText(timeText);
}
}
我的时间选择对话框对话框如下
public class dialog_timePicker extends DialogFragment {
private TimePickerDialog.OnTimeSetListener listener;
public dialog_timePicker() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_timepicker,container,false);
TextView mTextView = view.findViewById(R.id.setTime);
return view;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), listener, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
}