2016-05-15 43 views
2

這是我的代碼。給一些代碼如何禁用此代碼中的過去時間以及如何使用android時間選擇器對話框應用此操作。時間選取器對話框類沒有任何setMinTime方法以及如何獲取它。如何禁用在android中定時器選擇器對話框上的過去時間

time.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Calendar mcurrentTime = Calendar.getInstance(); 
      int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY); 
      int minute = mcurrentTime.get(Calendar.MINUTE); 

      TimePickerDialog mTimePicker; 

      mTimePicker = new TimePickerDialog(
        FillServiceDetailActivity.this, 
        new TimePickerDialog.OnTimeSetListener() { 
         @Override 
         public void onTimeSet(TimePicker timePicker, 
               int selectedHour, int selectedMinute) { 
          String AM_PM; 
          int hour; 
          String[] sp=currentTime.split(":"); 

          int hour1=Integer.parseInt(sp[0]); 
          int min1=Integer.parseInt(sp[1]); 
          //Toast.makeText(FillServiceDetailActivity.this,"selected hour:\t"+String.valueOf(selectedHour),Toast.LENGTH_LONG).show(); 
          String timeSet = ""; 
          if (selectedHour > 12 && hour1>12) { 
           selectedHour -= 12; 
           hour1-=12; 
           timeSet = "PM"; 
          } else if (selectedHour == 0) { 
           selectedHour += 12; 

           timeSet = "AM"; 
          } else if (selectedHour == 12) 

           timeSet = "PM"; 
          else 
           timeSet = "AM"; 


          String minutes = ""; 
          if (selectedMinute < 10) 
           minutes = "0" + selectedMinute; 
          else 
           minutes = String.valueOf(selectedMinute); 

          /*if(selectedHour>12) { 

           hour = selectedHour - 12; 
            AM_PM = "PM"; 

          } else if(selectedHour==12) 
          { 
           hour=selectedHour; 
           AM_PM="PM"; 
          } 
          else { 
           hour=selectedHour; 
           AM_PM="AM"; 
          }*/ 

          //Toast.makeText(FillServiceDetailActivity.this,String.valueOf(hour1),Toast.LENGTH_LONG).show(); 
          //Toast.makeText(FillServiceDetailActivity.this,String.valueOf(selectedHour),Toast.LENGTH_LONG).show(); 

           time.setText(selectedHour + ":" + minutes + "\t" + timeSet); 




         } 
        }, hour, minute, false);// Yes 24 hour time 
      mTimePicker.setTitle("Select Time"); 

      mTimePicker.show(); 


     } 
    }); 
+1

的可能的複製[安卓TimePickerDialog設置最大時間(http://stackoverflow.com/questions/13516389/android-timepickerdialog-set-max-time) –

回答

5

欲瞭解更多信息,請訪問page

使用此庫。

enter image description here

依賴

dependencies { 
     compile 'com.wdullaer:materialdatetimepicker:2.3.0' 
    } 

使用它來顯示對話框

Calendar now = Calendar.getInstance(); 
       TimePickerDialog tpd = TimePickerDialog.newInstance(
         MainActivity.this, 
         now.get(Calendar.HOUR_OF_DAY), 
         now.get(Calendar.MINUTE), 
         mode24Hours.isChecked() 
       ); 
       tpd.setThemeDark(modeDarkTime.isChecked()); 
       tpd.vibrate(vibrateTime.isChecked()); 
       tpd.dismissOnPause(dismissTime.isChecked()); 
       tpd.enableSeconds(enableSeconds.isChecked()); 
       if (modeCustomAccentTime.isChecked()) { 
        tpd.setAccentColor(Color.parseColor("#9C27B0")); 
       } 
       if (titleTime.isChecked()) { 
        tpd.setTitle("TimePicker Title"); 
       } 
       if (limitTimes.isChecked()) { 
        tpd.setTimeInterval(2, 5, 10); 
       } 
       tpd.setOnCancelListener(new DialogInterface.OnCancelListener() { 
        @Override 
        public void onCancel(DialogInterface dialogInterface) { 
         Log.d("TimePicker", "Dialog was cancelled"); 
        } 
       }); 
       tpd.show(getFragmentManager(), "Timepickerdialog"); 

從對話框獲取時間,當用戶選擇的時間。

@Override 
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute) { 
    String time = "You picked the following time: "+hourOfDay+"h"+minute; 
    timeTextView.setText(time); 
} 

使用此控制設置最小時間

setMinTime(Timepoint time) 

例如

tpd.setMinTime(now2.get(Calendar.HOUR_OF_DAY),now2.get(Calendar.MINUTE), 
         now2.get(Calendar.SECOND)); 

在上面的例子中是MinTime當前時間。

方法setMinTime有三個參數小時,分鐘,秒;

setMinTime(5,0,0); // By using this minTime is set to 5 'o clock.

+0

如何使用SetMinTime()什麼是時間點嗎? – rohit

+0

我編輯了我的答案。如果問題退出讓我知道。 –

相關問題