这就是我的时间选择器当前的样子。 我希望它只显示分钟和分钟,最多只能显示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);
}
答案 0 :(得分:4)
这就是我用最大值和最小值以及5分钟步骤完成的部分,在stackoverflow上有样本但是使用他们的方法你不能同时使用向上和向下按钮,我的方法可以工作:
public class Powernap extends Activity {
/** Called when the activity is first created. */
@Override
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.setCurrentMinute(5);
System.out.println("timepicker erstellt");
pn.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
System.out.println("Tastatur geblockt");
pn.setOnTimeChangedListener(StartTimeChangedListener);
System.out.println("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 && minute <= 59){
System.out.println("Minute+30");
nextMinute = 30;
}
else if(minute == 26){
System.out.println("Minute auf 30");
nextMinute = 30;
}
else if(minute == 25 || minute == 21 || minute == 29){
System.out.println("Minute auf 25");
nextMinute = 25;
}
else if(minute == 20 || minute == 16 || minute == 24){
System.out.println("Minute auf 20");
nextMinute = 20;
}
else if(minute == 15 || minute == 11 || minute == 19){
System.out.println("Minute auf 15");
nextMinute = 15;
}
else if(minute == 10 || minute == 6 || minute == 14){
System.out.println("Minute auf 10");
nextMinute = 10;
}
else if(minute == 5 || minute == 1 || minute == 9 || minute < 5){
System.out.println("Minute auf 5");
nextMinute = 5;
}
if (hourOfDay != 0){
System.out.println("hour>0");
nextHour = 24;
}
// remove ontimechangedlistener to prevent stackoverflow/infinite loop
timePicker.setOnTimeChangedListener(NullTimeChangedListener);
// set minute
timePicker.setCurrentMinute(nextMinute);
//set hour
timePicker.setCurrentHour(nextHour);
// hook up ontimechangedlistener again
timePicker.setOnTimeChangedListener(StartTimeChangedListener);
}
答案 1 :(得分:2)
您需要编写自己的小部件。 TimePicker
窗口小部件或TimePickerDialog
不支持您想要的内容。