2013-05-02 54 views
0

我有一個自定義首選項,單擊時顯示TimePicker對話框。我希望在選擇顯示所選時間的值時爲首選項設置摘要。這怎麼能做到?我不明白如何使用setSummary(CharSequence)方法。DialogPreference setSummary()

自定義偏好:

public class TimePreference extends DialogPreference { 

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

     private TimePicker timePicker; 

     public TimePreference(Context context, AttributeSet attributes) { 
     super(context, attributes); 
     setPersistent(false); 
     } 

     @Override 
     public void onBindDialogView(View view) { 
     super.onBindDialogView(view); 

     timePicker = (TimePicker) view.findViewById(R.id.prefTimePicker); 
     timePicker.setCurrentHour(getSharedPreferences().getInt(getKey() + ".hour", hour)); 
     timePicker.setCurrentMinute(getSharedPreferences().getInt(getKey() + ".minute", minute)); 
     timePicker.setIs24HourView(DateFormat.is24HourFormat(timePicker.getContext())); 
     } 

     @Override 
     protected void onDialogClosed(boolean okToSave) { 
     super.onDialogClosed(okToSave); 
     if (okToSave) { 
      timePicker.clearFocus(); 
      SharedPreferences.Editor editor = getEditor(); 
      editor.putInt(getKey() + ".hour", timePicker.getCurrentHour()); 
      editor.putInt(getKey() + ".minute", timePicker.getCurrentMinute()); 
      editor.commit(); 
      // Here should be the setSummary() method, maybe? 
     } 
     } 
} 

主要活動:在您的DialogPreference構造

public class MainActivity extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    getFragmentManager().beginTransaction().replace(android.R.id.content, 
      new PrefsFragment()).commit(); 
    } 
} 
+0

見http://stackoverflow.com/questions/ 531427 /如何-DO-I-顯示的電流 - 值的-AN-機器人偏好合的偏好蘇 – 2013-05-02 21:42:09

回答

0

使用以下行

setTitle("Your Preference Name goes here"); 
setSummary("Your summary goes here")