2015-09-19 171 views
4

如何控制TimePicker,我只想在時間範圍內顯示時間選擇器從上午8:00到下午2:00(除從上午08:00到下午2:00隱藏所有)限時間範圍時間選擇器

我寫了完整的代碼來顯示TimePicker和定製時間。

這裏是我的代碼:

  case DIALOG_TIME: 
      final Calendar c = Calendar.getInstance(); 
      int hour = c.get(Calendar.HOUR_OF_DAY); 
      int minute = c.get(Calendar.MINUTE); 

      // Create a new instance of TimePickerDialog and return it 
      return new TimePickerDialog(this, lisTime, hour, minute, 
      DateFormat.is24HourFormat(MainActivity.this)); 

     TimePickerDialog.OnTimeSetListener lisTime = new TimePickerDialog.OnTimeSetListener() { 

     @Override 
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
      // TODO Auto-generated method stub 
      String meridiem = ""; 
      nim = minute; 
      Calendar datetime = Calendar.getInstance(); 
      datetime.set(Calendar.HOUR_OF_DAY, hourOfDay); 
      datetime.set(Calendar.MINUTE, minute); 

      if (datetime.get(Calendar.AM_PM) == Calendar.AM) 
       meridiem = "AM"; 
      else if (datetime.get(Calendar.AM_PM) == Calendar.PM) 
       meridiem = "PM"; 

      hour = (datetime.get(Calendar.HOUR) == 0) ?"12":String.valueOf(datetime.get(Calendar.HOUR)); 

      String time = pad(Integer.parseInt(hour)) + ":" + pad(minute) + " " + meridiem; 
      editTime.setText(time); 

     } 
    }; 

    private String pad(int value){ 

     if(value<10){ 
      return "0"+value; 
     } 

     return ""+value; 
    } 
+0

@sasikumar我不知道如何限制時間timepicker?我仍然無法控制它...顯示全部24小時,我只想顯示上午08:00至下午02:00 – Sun

+0

據我所知,沒有辦法限制TimePicker的時間範圍'小部件。你將不得不創建自己的。 – Squonk

+0

其簡單的參考http://stackoverflow.com/questions/20214547/show-timepicker-with-minutes-intervals-in-android – sasikumar

回答

1

正如這個答案:https://stackoverflow.com/a/20396673/4130107 您可以創建自定義TimePickerDialog並覆蓋onAttachedToWindow();而是限制小時範圍:

private final boolean mIs24HourView; 

public CustomTimePickerDialog(Context context, OnTimeSetListener callBack, 
           int hourOfDay, int minute, boolean is24HourView) { 
    super(context, callBack, hourOfDay, minute, is24HourView); 
    mIs24HourView = is24HourView; 
} 

@Override 
public void onAttachedToWindow() { 
    super.onAttachedToWindow(); 
    try { 
     Class<?> classForid = Class.forName("com.android.internal.R$id"); 
     Field timePickerField = classForid.getField("timePicker"); 
     this.timePicker = (TimePicker) findViewById(timePickerField 
       .getInt(null)); 
     Field field = classForid.getField("hour"); 


     final NumberPicker mHourSpinner = (NumberPicker) timePicker 
       .findViewById(field.getInt(null)); 

     if (mIs24HourView) { 
      mHourSpinner.setMinValue(2); 
      mHourSpinner.setMaxValue(20); 
     }else { 
      Field amPm = classForid.getField("amPm"); 

      mHourSpinner.setMinValue(2); 
      final NumberPicker amPm = (NumberPicker) timePicker 
        .findViewById(amPm.getInt(null)); 
      amPm.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() { 
       @Override 
       public void onValueChange(NumberPicker np1, int oldVal, int newVal) { 
        if (newVal == 0) { // case AM 
         mHourSpinner.setMinValue(2); 
         mHourSpinner.setMaxValue(12); 
        } else { // case PM 
         mHourSpinner.setMinValue(1); 
         mHourSpinner.setMaxValue(8); 
        } 
       } 
      }); 

     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

編輯:現在工作了AM/PM太

enter image description here