2016-11-08 235 views
-1

我的問題是我需要間隔5分鐘來實現TimePicker。Android TimePicker間隔5分鐘

所有的例子,我是這裏尋找

Android - TimePicker minutes to 15

這裏

How do i change the Android TimePicker minute intervals?

對我不起作用。

如果可以,我希望看到一個完整的工作示例。

此代碼的工作,但我不知道如何通過這裏安裝的時候,不知道怎麼弄的時間

import android.content.Context; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.NumberPicker; 
import android.widget.TextView; 
import android.widget.TimePicker; 
import java.lang.reflect.Field; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.List; 

public class MainActivity extends AppCompatActivity { 

    TimePicker pickStartTime; 
    TextView textwiew; 
    private int TIME_PICKER_INTERVAL = 5; 
    NumberPicker minutePicker; 
    List<String> displayedValues; 


    final Calendar cal = Calendar.getInstance(); 

    private int myHourS = cal.get(Calendar.HOUR_OF_DAY); 
    private int myMinuteS = cal.get(Calendar.MINUTE); 

    int nextMinute = 0; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     textwiew =(TextView)findViewById(R.id.text1); 

     pickStartTime = (TimePicker)findViewById(R.id.timePicker1); 
     pickStartTime.setIs24HourView(true); 

     setTimePickerInterval(pickStartTime); 
    } 


    private void setTimePickerInterval(TimePicker timePicker) { 
     try { 
      Class<?> classForid = Class.forName("com.android.internal.R$id"); 
      // Field timePickerField = classForid.getField("timePicker"); 

      Field field = classForid.getField("minute"); 
      minutePicker = (NumberPicker) timePicker 
        .findViewById(field.getInt(null)); 

      minutePicker.setMinValue(0); 
      minutePicker.setMaxValue(11); 
      displayedValues = new ArrayList<String>(); 
      for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) { 
       displayedValues.add(String.format("%02d", i)); 
      } 
      // for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) { 
      //  displayedValues.add(String.format("%02d", i)); 
      // } 
      minutePicker.setDisplayedValues(displayedValues 
        .toArray(new String[0])); 
      minutePicker.setWrapSelectorWheel(true); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void onClick (View v) { 
     int Minut = pickStartTime.getCurrentMinute(); 
     textwiew.setText(Minut); 
    } 


} 

回答

0

不知道怎麼弄的時間

使用OnTimeChangedListener

pickStartTime = (TimePicker)findViewById(R.id.timePicker1); 
pickStartTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { 

    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { 
     Log.d("Time", "You picked " 
       + String.valueOf(view.getCurrentHour()) + ":" 
       + String.valueOf(view.getCurrentMinute())); 
    } 

}); 
+0

謝謝你,我的作品。你能告訴我如何設定當前時間嗎? –

+0

setHour和setMinute ...從[文檔](https://developer.android.com/reference/android/widget/TimePicker.html) –