0

单击时,我有一个自定义首选项显示 TimePicker 对话框。I want to set the summary for the preference when a value is choosed displaying the chosen time. 怎么可能做到这一点?我不明白如何使用 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();
  }
}
4

1 回答 1

0

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

setTitle("Your Preference Name goes here");
setSummary("Your summary goes here")
于 2013-12-12T20:13:17.710 回答