开发者

How do you create start time & end time time pickers from preferences?

I would lik开发者_如何学JAVAe to set up a time picker in a preference screen and allow the user to pick both a start time and also an end time and store it as a persistant value like other settings.

Could you show all the coding needed to set this up because I couldn't find anything like that by searching.

I'm thinking maybe the time picker should be in a dialog but I don't yet know how to set those up yet. If you can show coding that calls a dialog with a start and end time time picker and how to save that information for later retrieval from a preferences screen that would be great.

All help will be greatly appreciated.

Truly, Emad


I have a simple TimePreference that I use in one of my books:

import android.content.Context;
import android.content.res.TypedArray;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;

public class TimePreference extends DialogPreference {
  private int lastHour=0;
  private int lastMinute=0;
  private TimePicker picker=null;

  public static int getHour(String time) {
    String[] pieces=time.split(":");

    return(Integer.parseInt(pieces[0]));
  }

  public static int getMinute(String time) {
    String[] pieces=time.split(":");

    return(Integer.parseInt(pieces[1]));
  }

  public TimePreference(Context ctxt, AttributeSet attrs) {
    super(ctxt, attrs);

    setPositiveButtonText("Set");
    setNegativeButtonText("Cancel");
  }

  @Override
  protected View onCreateDialogView() {
    picker=new TimePicker(getContext());

    return(picker);
  }

  @Override
  protected void onBindDialogView(View v) {
    super.onBindDialogView(v);

    picker.setCurrentHour(lastHour);
    picker.setCurrentMinute(lastMinute);
  }

  @Override
  protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult) {
      lastHour=picker.getCurrentHour();
      lastMinute=picker.getCurrentMinute();

      String time=String.valueOf(lastHour)+":"+String.valueOf(lastMinute);

      if (callChangeListener(time)) {
        persistString(time);
      }
    }
  }

  @Override
  protected Object onGetDefaultValue(TypedArray a, int index) {
    return(a.getString(index));
  }

  @Override
  protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    String time=null;

    if (restoreValue) {
      if (defaultValue==null) {
        time=getPersistedString("00:00");
      }
      else {
        time=getPersistedString(defaultValue.toString());
      }
    }
    else {
      time=defaultValue.toString();
    }

    lastHour=getHour(time);
    lastMinute=getMinute(time);
  }
}

A production-grade implementation of this would use string resources for the two button captions. It might also have a bit more error checking on the value, in case some code updates it via SharedPreferences.Editor and does not format it properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜