我想从30 min interval
获得time picker dialog
,下面的代码在Android N
下可以正常工作,但不能在最新的设备上工作。以下是我的activity
代码。
public class MainActivity extends AppCompatActivity {
private TextView tvSelectedTime;
private Button btnOpentimePickerDialog;
private int intHour, intMinute;
private String strMin = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getIds();
setListner();
}
private void setListner() {
try {
btnOpentimePickerDialog.setOnClickListener(onClickListener);
} catch (Exception e) {
e.printStackTrace();
}
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
switch (view.getId()){
case R.id.btnOpentimePickerDialog:
openTimePicker();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
private void openTimePicker() {
CustomTimePickerDialog timePickerDialog = new CustomTimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@SuppressLint("NewApi")
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Log.e("Log", "selected time----" + hourOfDay + ":" + minute);
String strTime = hourOfDay + ":"+ minute + ":00";
updateTime(hourOfDay, minute);
}
}, intHour, intMinute, false);
timePickerDialog.show();
}
private void updateTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12)
timeSet = "PM";
else
timeSet = "AM";
//TODO=================Adding zero in one dightleft and right======================
if (mins < 10)
strMin = "0" + mins;
else
strMin = String.valueOf(mins);
// Append in a StringBuilder
String aTime = new StringBuilder().append(pad(hours)).append(':')
.append(pad(Integer.parseInt(strMin))).append(" ").append(timeSet).toString();
Log.e("aTime checking ==>",""+aTime);
tvSelectedTime.setText(aTime);
}
private void getIds() {
try {
btnOpentimePickerDialog = findViewById(R.id.btnOpentimePickerDialog);
tvSelectedTime = findViewById(R.id.tvSelectedTime);
} catch (Exception e) {
e.printStackTrace();
}
}
private String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
和下面是我的自定义time picker dialog code
。
public class CustomTimePickerDialog extends TimePickerDialog {
private final static int TIME_PICKER_INTERVAL = 30;
private TimePicker mTimePicker;
private final OnTimeSetListener mTimeSetListener;
public CustomTimePickerDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute, boolean is24HourView) {
super(context, TimePickerDialog.THEME_HOLO_LIGHT, null, hourOfDay,
minute / TIME_PICKER_INTERVAL, is24HourView);
mTimeSetListener = listener;
}
@Override
public void updateTime(int hourOfDay, int minuteOfHour) {
mTimePicker.setCurrentHour(hourOfDay);
mTimePicker.setCurrentMinute(minuteOfHour / TIME_PICKER_INTERVAL);
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case BUTTON_POSITIVE:
if (mTimeSetListener != null) {
mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
mTimePicker.getCurrentMinute() * TIME_PICKER_INTERVAL);
}
break;
case BUTTON_NEGATIVE:
cancel();
break;
}
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
Field timePickerField = classForid.getField("timePicker");
mTimePicker = (TimePicker) findViewById(timePickerField.getInt(null));
Field field = classForid.getField("minute");
NumberPicker minuteSpinner = (NumberPicker) mTimePicker
.findViewById(field.getInt(null));
minuteSpinner.setMinValue(0);
minuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
List<String> displayedValues = new ArrayList<>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
minuteSpinner.setDisplayedValues(displayedValues
.toArray(new String[displayedValues.size()]));
} catch (Exception e) {
e.printStackTrace();
}
}
}
在某些设备中,上面的代码可以正常工作,screenshot
就是这样。
在某些设备上,我无法通过30 min interval
抽空,如下所示。
实际上,我想要实现time picker
就像第一个屏幕一样,该屏幕在所有设备中都支持。
答案 0 :(得分:3)
我已经使用此library解决了我的问题,这是我完整的code,将来可能会对您有所帮助。
我的完整代码在下面。
public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {
private TextView timeTextView;
private TimePickerDialog timePickerDialog;
private Button timeButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find our View instances
timeTextView = findViewById(R.id.time_textview);
timeButton = findViewById(R.id.time_button);
// Show a timepicker when the timeButton is clicked
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
timePickerDialog = TimePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
false
);
timePickerDialog.setThemeDark(false);
timePickerDialog.setTitle("TimePicker Title");
timePickerDialog.setTimeInterval(1, 30, 60);
timePickerDialog.setAccentColor(Color.parseColor("#9C27B0"));
timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface)
Log.d("TimePicker", "Dialog was cancelled");
}
});
timePickerDialog.show(getFragmentManager(), "Timepickerdialog");
}
});
}
@Override
public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {
updateEndTime(hourOfDay,minute);
}
private void updateEndTime(int hours, int mins) {
String timeSet = "";
if (hours > 12) {
hours -= 12;
timeSet = "PM";
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
} else if (hours == 12) {
timeSet = "PM";
} else {
timeSet = "AM";
}
// Append in a StringBuilder
String aTime = new StringBuilder().append(pad(hours)).append(':')
.append(pad(mins)).append(" ").append(timeSet).toString();
timeTextView.setText(aTime);
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
@Override
public void onResume() {
super.onResume();
TimePickerDialog tpd = (TimePickerDialog) getFragmentManager().findFragmentByTag("Timepickerdialog");
if (tpd != null) tpd.setOnTimeSetListener(this);
}
}
我的愿望输出是这样的: