我正在我的Android应用程序中实现时间选择器对话框。我想在该对话框中设置3个按钮,"设置","清除","取消"。
我正在使用TimePickerFragment,我已经为此完成了代码,但它无法在所有Android设备上正常工作,在某些设备上它的工作完美,但在某些设备上却没有。
我使用以下代码: -
public class TimePickerFragment extends DialogFragment implements
OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
final TimePickerDialog timePickerDialog = new TimePickerDialog(
getActivity(), this, hour, min, true);
timePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE,
getActivity().getResources().getString(R.string.done1),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
isTimeSet = true;
System.out.println("only true");
}
});
timePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
getActivity().getResources().getString(R.string.cancle),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
isTimeSet = false;
System.out.println("only false");
//tvTime1.setText(userAlarmStartTime);
}
});
timePickerDialog.setButton(DialogInterface.BUTTON_NEUTRAL,
getActivity().getResources().getString(R.string.clear),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
isTimeSet = false;
System.out.println("only neutral");
tvTime1.setText("");
}
});
return timePickerDialog;
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
// System.out.println("value in time set:- " + isTimeSet);
if (isTimeSet) {
String userAlarmStartTime;
if (minute < 10) {
userAlarmStartTime = hourOfDay + ":0" + minute;
} else {
userAlarmStartTime = hourOfDay + ":" + minute;
}
tvTime1.setText(userAlarmStartTime);
}
}
}
并且为了调用它,我使用以下代码: -
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
当我在某些设备上调试我的代码时,它没有调用onTimeSet()
方法,但在某些设备上调用该方法。
我已经搜索了很多关于此的内容,但我找不到任何可以帮助我解决问题的内容。
任何人都可以告诉我哪里出错了?