问题描述
我想从 time picker对话框
中获取 30分钟间隔
,下面的代码在 Android N
下正常运行,但在最新的设备上无法正常工作.以下是我的活动
代码.
公共类MainActivity扩展了AppCompatActivity {私人TextView tvSelectedTime;私人按钮btnOpentimePickerDialog;private intHour,intMinute;private String strMin =";@Override受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getIds();setListner();}私人无效setListner(){尝试 {btnOpentimePickerDialog.setOnClickListener(onClickListener);} catch(Exception e){e.printStackTrace();}}View.OnClickListener onClickListener = new View.OnClickListener(){@Overridepublic void onClick(View view){尝试 {切换(view.getId()){案例R.id.btnOpentimePickerDialog:openTimePicker();休息;}} catch(Exception e){e.printStackTrace();}}};私人void openTimePicker(){CustomTimePickerDialog timePickerDialog =新的CustomTimePickerDialog(MainActivity.this,新的TimePickerDialog.OnTimeSetListener(){@SuppressLint("NewApi")@Overridepublic void onTimeSet(TimePicker view,int hourOfDay,int minutes){Log.e("Log","selected --------" + hourOfDay +:" +分钟);字符串strTime = hourOfDay +:" +分钟+:00";updateTime(hourOfDay,分钟);}},intHour,intMinute,false);timePickerDialog.show();}私人void updateTime(int hours,int mins){字符串timeSet =";如果(小时> 12){小时-= 12;timeSet ="PM";}否则,如果(小时== 0){小时+ = 12;timeSet ="AM";} else if(小时== 12)timeSet ="PM";别的timeSet ="AM";//TODO =================在一个左和右加零======================如果(分钟
和下面是我的自定义时间选择器对话框代码
.
公共类CustomTimePickerDialog扩展了TimePickerDialog {私有最终静态int TIME_PICKER_INTERVAL = 30;私人TimePicker mTimePicker;私有最终的OnTimeSetListener mTimeSetListener;public CustomTimePickerDialog(Context context,OnTimeSetListener listener,int hourOfDay,int minutes,boolean is24HourView){超级(上下文,TimePickerDialog.THEME_HOLO_LIGHT,null,hourOfDay,分钟/TIME_PICKER_INTERVAL,is24HourView);mTimeSetListener =侦听器;}@Override公共无效updateTime(int hourOfDay,int minutesOfHour){mTimePicker.setCurrentHour(hourOfDay);mTimePicker.setCurrentMinute(minuteOfHour/TIME_PICKER_INTERVAL);}@Overridepublic void onClick(DialogInterface dialog,int which){切换(哪个){情况BUTTON_POSITIVE:如果(mTimeSetListener!= null){mTimeSetListener.onTimeSet(mTimePicker,mTimePicker.getCurrentHour(),mTimePicker.getCurrentMinute()* TIME_PICKER_INTERVAL);}休息;情况BUTTON_NEGATIVE:取消();休息;}}@Override公共无效onAttachedToWindow(){super.onAttachedToWindow();尝试 {类<?>classForid = Class.forName("com.android.internal.R $ id");字段timePickerField = classForid.getField("timePicker");mTimePicker =(TimePicker)findViewById(timePickerField.getInt(null));字段字段= classForid.getField("minute");NumberPicker minutesSpinner =(NumberPicker)mTimePicker.findViewById(field.getInt(null));minutesSpinner.setMinValue(0);minutesSpinner.setMaxValue((60/TIME_PICKER_INTERVAL)-1);List< String>displayValues = new ArrayList<>();对于(int i = 0; i< 60; i + = TIME_PICKER_INTERVAL){displayValues.add(String.format(%02d",i));}minutesSpinner.setDisplayedValues(displayedValues.toArray(new String [displayedValues.size()]));} catch(Exception e){e.printStackTrace();}}}
在某些设备上,上面的代码可以正常工作,而这是截屏
.
在某些设备中,我无法按 30分钟间隔
来获取时间,如下所示.
实际上,我想实现时间选择器
,如在所有设备上都支持的第一个屏幕.
我已经使用此
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.
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:
这篇关于如何在Android N中获得30分钟的时间选择器间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!