DialogPreference setSummary()

时间:2013-05-02 16:47:53

标签: java android preferenceactivity

单击时,我有一个自定义首选项显示TimePicker对话框。我想在选择显示所选时间的值时设置首选项的摘要。怎么可以这样做?我不明白如何使用setSummary(CharSequence)方法。

自定义偏好:

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?
        }
      } 
}

主要活动:

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

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

1 个答案:

答案 0 :(得分:0)

在DialogPreference构造函数中使用以下行

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