2016-04-26 72 views
0

我在我的設置屏幕中有一個時間選擇器對話框。當用戶點擊時間選擇器對話時,用戶選擇時間。但是當用戶再次點擊時間選擇器時,時間選擇器對話將顯示當前時間而不是所選時間。 這是我到目前爲止所做的。設置時間選擇器對話到選定時間

mreminder.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Calendar mcurrentTime = Calendar.getInstance(); 
      final int[] hour = {mcurrentTime.get(Calendar.HOUR_OF_DAY)}; 
      int minute = mcurrentTime.get(Calendar.MINUTE); 
      TimePickerDialog mTimePicker; 
      mTimePicker = new TimePickerDialog(SettingsActivity.this, new TimePickerDialog.OnTimeSetListener() { 
       @Override 
       public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { 
        int hour = selectedHour; 
        int minutes = selectedMinute; 
        String timeSet = " "; 
        if (hour > 12) { 
         hour -= 12; 
         timeSet = "PM"; 
        } else if (hour == 0) { 
         hour += 12; 
         timeSet = "AM"; 
        } else if (hour == 12) 
         timeSet = "PM"; 
        else 
         timeSet = "AM"; 

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

        // Append in a StringBuilder 
        String aTime = new StringBuilder().append(hour).append(':') 
          .append(min).append(" ").append(timeSet).toString(); 
        mreminder.setText(aTime); 
        pref.savePref(REMINDER, aTime); 
       } 
      }, hour[0], minute, false); 
      mTimePicker.setTitle("Select Time"); 
      mTimePicker.show(); 

     } 
    }); 

有人能告訴我如何在下次用戶點擊它時將timepicker設置爲所選時間。

+1

[Android:在文本視圖中顯示時間設置時間選擇器]可能的重複(http://stackoverflow.com/questions/12494074/android-setting-time-in-time-picker-with-the顯示在文本視圖中) –

+0

[如何在TimePicker中設置自定義時間]的可能重複(http://stackoverflow.com/questions/31376072/how-to-set-custom-time-in-timepicker ) –

+0

我不能得到它,你可以告訴我關於我的代碼。 –

回答

0

將選定時間存儲在sharedPreferences中,並在onClick事件中檢查sharedPreference是否有東西。如果是有一些它然後加載從那裏數據 你可以做這樣的事情:

int hours,mins;//global variables;  
mreminder.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     int h = Integer.parseInt(sharedPreference.getString("HOUR","0)); 
     int m = Integer.parseInt(sharedPreference.getString("MIN","0"));//"0" is the default value if nothing is stored; 
     if(h>0){ 
       hours = h; 
       mins = m; 
     }else{ 
      Calendar mcurrentTime = Calendar.getInstance(); 
     hours = mcurrentTime.get(Calendar.HOUR_OF_DAY); 
     mins = mcurrentTime.get(Calendar.MINUTE); 
     } 

     TimePickerDialog mTimePicker; 
     mTimePicker = new TimePickerDialog(SettingsActivity.this, new TimePickerDialog.OnTimeSetListener() { 
      @Override 
      public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) { 
       int hour = selectedHour; 
       int minutes = selectedMinute; 
       String timeSet = " "; 
       if (hour > 12) { 
        hour -= 12; 
        timeSet = "PM"; 
       } else if (hour == 0) { 
        hour += 12; 
        timeSet = "AM"; 
       } else if (hour == 12) 
        timeSet = "PM"; 
       else 
        timeSet = "AM"; 

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

       // Append in a StringBuilder 
       String aTime = new StringBuilder().append(hour).append(':') 
         .append(min).append(" ").append(timeSet).toString(); 
       mreminder.setText(aTime); 
       pref.savePref(REMINDER, aTime); 
      } 
     }, hours, mins, false); 
     mTimePicker.setTitle("Select Time"); 
     mTimePicker.show(); 

    } 
}); 

這將解決您的問題。

+0

它不適合我。 –

+0

請提供sharedprefrences的代碼...以便我可以幫助您 – Ashwani

+0

我正在將提示保存在提醒中。在上面的代碼中 –