2017-12-08 1 views
0

タイムピッカーを選んだ後にカレンダーの時間を設定するにはどうすればよいですか?私が選ぶと、カレンダーの時間は動かないようです。私が午後1時を選ぶと、それは永遠に午後1時です。 1分後に午後1時01分にはどうすればいいですか?私はタイムピッカーの後に、私はカレンダーに設定しました。タイムピック後にカレンダーの時間を続ける

時間+経過時間私が正しくあなたの質問を理解していれば、ユーザーがTimePickerダイアログを使用してクロックを設定できるようにしたい、とTimePickerダイアログショーに前のセットを持って、私の英語

答えて

0

のため申し訳ありませんが、次のそれが示す時間? これはあなたのために働く可能性があります

MainActivity.java

// class implements TimePickerDialog.OnTimeSetListener and uses onTimeSet() 
public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener { 
    //myTime wrapped in Long instead of primitive long so it can be null 
    Long myTime; 
    int hours, mins; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    } 

    //TimePickerDialog.OnTimeSetListener returns here 
    @Override 
    public void onTimeSet(TimePicker view, int hour, int min) { 
    myTime = System.currentTimeMillis(); 
    this.hours = hour; 
    this.mins = min; 
    } 

    //our xml button onClick method to trigger TimePicker dialog 
    public void timeButtonClicked(View v){ 
    //send bundle arguments to fragment if hour/minute previously set 
    TimePickerFragment fragment = TimePickerFragment.newInstance(myTime, hours, mins); 
    fragment.show(getFragmentManager(), "timePicker"); 
    } 
} 

TimePickerFragment.java

public class TimePickerFragment extends DialogFragment { 

    public static TimePickerFragment newInstance(Long savedTime, int hours, int mins){ 
    TimePickerFragment myFragment = new TimePickerFragment(); 
    Bundle args = new Bundle(); 
    //savedTime is Long wrapped so it can be null if not set 
    //and thus bundle will not be set if savedTime is null 
    if(savedTime!=null){ 
     args.putLong("savedTime", savedTime); 
     args.putInt("hours", hours); 
     args.putInt("mins", mins); 
     myFragment.setArguments(args); 
    } 
    return myFragment; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState){ 
    Long savedTime; 
    int hoursDiff=0, minsDiff=0; 
    int hourOfDay, minute; 

    // create a Calendar instance of the current time 
    Calendar c = Calendar.getInstance(); 
    long currentTime = c.getTimeInMillis(); 

    // create hourOfDay, minute variables to pass as parameters 
    // to the TimePicker dialog. If passed by bundle, use that 
    // time and difference otherwise use current time 
    Bundle bundle = getArguments(); //NOT savedInstanceState bundle 
    if (bundle != null) { 
     // unix time in milliseconds since Epoch set 
     // by System.currentTimeMillis() by MainActivity.onTimeSet() 
     savedTime = bundle.getLong("savedTime"); 
     // set calendar to passed savedTime + (current time difference) 
     long timeDifference = currentTime-savedTime; 
     minsDiff = (int)(timeDifference/60000); 
     if(minsDiff>60) hoursDiff = (int)(minsDiff/60); 
     // hours and mins last set by MainActivity.onTimeSet() 
     hourOfDay=bundle.getInt("hours")+hoursDiff; 
     minute=bundle.getInt("mins")+minsDiff; 
    } 
    else { 
     // bundle not set, use current time 
     hourOfDay = c.get(Calendar.HOUR_OF_DAY); 
     minute = c.get(Calendar.MINUTE); 
    } 

    // Create a new instance of TimePickerDialog with hourOfDay and minute parameters and return the fragment 
    return new TimePickerDialog(getActivity(), 
     (TimePickerDialog.OnTimeSetListener) getActivity(), hourOfDay, minute, 
     DateFormat.is24HourFormat(getActivity())); 
    } 
} 
関連する問題