问题
I want to get 30 min interval
from time picker dialog
and below code works proper below Android N
but not working in latest device. Below is my code for 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);
}
}
and below is my custom 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();
}
}
}
In some device, above code is working fine and here is that screenshot
.
and in some device I am not able to get time by 30 min interval
like below.
Actually I want to implement time picker
like first screen which support in all device.
回答1:
I have resolved my issue using this library and here is my complete code which may help someone in the future.
My complete code is in below.
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);
}
}
My desire output is like this:
来源:https://stackoverflow.com/questions/51555842/how-to-get-30-min-interval-in-time-picker-in-android-n