如果用户使用键盘输入时间,TimePicker 仅显示分钟并设置最大分钟
这就是我的时间选择器目前的样子。 我希望它只显示分钟,并且最多只显示 30 分钟。 它工作正常,但是当你用键盘输入时间时,它会将时间设置为 0,但我希望它达到最大值。 因此,如果用户输入 50,它应该变为 30,我该怎么做? 我怎样才能隐藏时间选择器的时间,因为这里不需要它。
protected void onCreate(Bundle savedInstanceState) {
System.out.println("Powernap");
super.onCreate(savedInstanceState);
setContentView(R.layout.powernap);
TimePicker pn = (TimePicker) findViewById(R.id.tp_powernap);
pn.setOnTimeChangedListener(StartTimeChangedListener);
pn.setIs24HourView(true);
}
private TimePicker.OnTimeChangedListener StartTimeChangedListener =
new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
updateDisplay(view, hourOfDay, minute);
}
};
private TimePicker.OnTimeChangedListener NullTimeChangedListener =
new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
}
};
private void updateDisplay(TimePicker timePicker, int hourOfDay, int minute) {
// do calculation of next time
int nextMinute = 0;
int nextHour = 0;
if (minute > 30){
System.out.println("Minute+30");
nextMinute = 30;
// set minute
timePicker.setCurrentMinute(nextMinute);
}
else {
System.out.println("minute 0");
nextMinute = 0;
}
if (hourOfDay != 0){
System.out.println("hour>0");
nextHour = 24;
}
// remove ontimechangedlistener to prevent stackoverflow/infinite loop
timePicker.setOnTimeChangedListener(NullTimeChangedListener);
// set minute
timePicker.setCurrentMinute(timePicker.getCurrentMinute()+nextMinute);
//set hour
timePicker.setCurrentHour(nextHour);
// hook up ontimechangedlistener again
timePicker.setOnTimeChangedListener(StartTimeChangedListener);
}
This is how my timepicker looks at the moment.
I want it to only display the minutes and the minutes only to maximum 30.
It works fine but when u enter the time with the keyboard it sets the time to 0 but i want it to go to the maximum.
so if a user enters 50 it should go to 30, how can i do this?
and how can i hide the hour of the timepicker cause it's not needed here.
protected void onCreate(Bundle savedInstanceState) {
System.out.println("Powernap");
super.onCreate(savedInstanceState);
setContentView(R.layout.powernap);
TimePicker pn = (TimePicker) findViewById(R.id.tp_powernap);
pn.setOnTimeChangedListener(StartTimeChangedListener);
pn.setIs24HourView(true);
}
private TimePicker.OnTimeChangedListener StartTimeChangedListener =
new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
updateDisplay(view, hourOfDay, minute);
}
};
private TimePicker.OnTimeChangedListener NullTimeChangedListener =
new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
}
};
private void updateDisplay(TimePicker timePicker, int hourOfDay, int minute) {
// do calculation of next time
int nextMinute = 0;
int nextHour = 0;
if (minute > 30){
System.out.println("Minute+30");
nextMinute = 30;
// set minute
timePicker.setCurrentMinute(nextMinute);
}
else {
System.out.println("minute 0");
nextMinute = 0;
}
if (hourOfDay != 0){
System.out.println("hour>0");
nextHour = 24;
}
// remove ontimechangedlistener to prevent stackoverflow/infinite loop
timePicker.setOnTimeChangedListener(NullTimeChangedListener);
// set minute
timePicker.setCurrentMinute(timePicker.getCurrentMinute()+nextMinute);
//set hour
timePicker.setCurrentHour(nextHour);
// hook up ontimechangedlistener again
timePicker.setOnTimeChangedListener(StartTimeChangedListener);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我用最大和最小以及 5 分钟步骤完成部分的方法,stackoverflow 上有示例,但使用他们的方法你不能同时使用向上和向下按钮,用我的方法它可以工作:
that's how i did the part with the maximum and minimum and the 5mins steps, there are samples on stackoverflow but with their methods u can't use both the up and the down button, with my method it works:
您将需要编写自己的小部件。
TimePicker
小部件或TimePickerDialog
不支持您想要的功能。You will need to write your own widget. What you want is not supported by the
TimePicker
widget orTimePickerDialog
.